大概在好幾年前就已經(jīng)聽過很多公司都在使用nosql數(shù)據(jù)庫,只是在最近一兩年才在自己的項目當中具體使用到,實際上每種技術(shù)的出現(xiàn)都是由于現(xiàn)有的技術(shù)無法滿足日益更新的需求,畢竟當今各行各業(yè)都是飛速發(fā)展,所以技術(shù)也是不斷迭代更新。
nosql數(shù)據(jù)庫實際上有很多種,可能我們常見的也就是redis、memcached、mongodb這幾種,實際上目前做任何項目當中能夠使用到這幾種也基本上可以滿足任何緩存或者大數(shù)據(jù)庫級別的業(yè)務(wù)。
mongodb是來做一些大數(shù)據(jù)儲存的,由于它的文檔儲存特性,可以存儲用戶的各種信息例如裝備、積分等信息,而且后期擴展時不需要加字段,只需要改變實體bean就實現(xiàn)了數(shù)據(jù)庫字段的自動擴展,再例如像系統(tǒng)中所有的日志記錄、每個用戶的點贊明細記錄、朋友圈所發(fā)布信息,都是mongodb運用的場景,在這里不過多敘訴,后期會有專門mongodb篇來介紹。
memcached實際上和redis是一個級別的緩存數(shù)據(jù)庫,但是本人認為redis在很多方面都是比較有優(yōu)勢,例如redis的各種豐富的查詢,數(shù)據(jù)的是持久化、及其它的主從復(fù)制和分片環(huán)境,可以實現(xiàn)故障時的無縫轉(zhuǎn)移。
很多人可能會有疑問,數(shù)據(jù)存到mysql或者oracle這樣數(shù)據(jù)庫不就可以了嗎,每次需要去數(shù)據(jù)庫取出來就行,為什么要是使用這些nosql數(shù)據(jù)庫。
1、首先redis既然是緩存級別的數(shù)據(jù),當時數(shù)據(jù)全部是存放在內(nèi)存當中的,好處就是快,讀取數(shù)據(jù)性能高,像一些經(jīng)常會使用到的地方或者頻繁訪問的地方是不可能每次的訪問數(shù)據(jù)庫的,這樣性能即低而且當并發(fā)量大的時候,數(shù)據(jù)庫也是承受不住的,這個時候使用redis來存放時最好不過了,例如整個APP的首頁的數(shù)據(jù),再例如系統(tǒng)中需要用到的一些基礎(chǔ)數(shù)據(jù)包括城市、景點等不會經(jīng)常變的數(shù)據(jù),或者是聊天的最近聯(lián)系人和聊天信息,都是它運用的場景。
2、傳統(tǒng)的數(shù)據(jù)庫msyql或者oracle,在海量數(shù)據(jù)時查詢的效率是相當?shù)偷模@個時候就需要用到mongodb大數(shù)據(jù)性能。
windows環(huán)境中redis連接和測試
1)因為只是本地熟悉操作,所以下載windows版本即可
2)下載完成后只需啟動redis-server.exe即可。
3)用本地測試類測試一下,是否正常
4)當然還需要導(dǎo)入redis相關(guān)的jar包
介紹完了簡單的windows中redis環(huán)境的測試,下面介紹redis各種類型的具體操作。
1)key功能
private?void?KeyOperate()
{
System.out.println("======================key==========================");
//?清空數(shù)據(jù)
System.out.println("清空庫中所有數(shù)據(jù):"+jedis.flushDB());????????//?判斷key否存在
System.out.println("判斷key999鍵是否存在:"+shardedJedis.exists("key999"));
System.out.println("新增key001,value001鍵值對:"+shardedJedis.set("key001",?"value001"));
System.out.println("判斷key001是否存在:"+shardedJedis.exists("key001"));????????//?輸出系統(tǒng)中所有的key
System.out.println("新增key002,value002鍵值對:"+shardedJedis.set("key002",?"value002"));
System.out.println("系統(tǒng)中所有鍵如下:");
Set?keys?=?jedis.keys("*");
Iterator?it=keys.iterator()?;
while(it.hasNext()){
String?key?=?it.next();
System.out.println(key);
}????????//?刪除某個key,若key不存在,則忽略該命令。
System.out.println("系統(tǒng)中刪除key002:?"+jedis.del("key002"));
System.out.println("判斷key002是否存在:"+shardedJedis.exists("key002"));????????//?設(shè)置?key001的過期時間
System.out.println("設(shè)置?key001的過期時間為5秒:"+jedis.expire("key001",?5));????????try{
Thread.sleep(2000);
}
catch?(InterruptedException?e){
}
//?查看某個key的剩余生存時間,單位【秒】.永久生存或者不存在的都返回-1
System.out.println("查看key001的剩余生存時間:"+jedis.ttl("key001"));????????//?移除某個key的生存時間
System.out.println("移除key001的生存時間:"+jedis.persist("key001"));
System.out.println("查看key001的剩余生存時間:"+jedis.ttl("key001"));????????//?查看key所儲存的值的類型
System.out.println("查看key所儲存的值的類型:"+jedis.type("key001"));????????/*
*?一些其他方法:1、修改鍵名:jedis.rename("key6",?"key0");
*?????????????2、將當前db的key移動到給定的db當中:jedis.move("foo",?1)?????????*/
}
運行結(jié)果:
======================key==========================
清空庫中所有數(shù)據(jù):OK
判斷key999鍵是否存在:false
新增key001,value001鍵值對:OK
判斷key001是否存在:true
新增key002,value002鍵值對:OK
系統(tǒng)中所有鍵如下:
key002
key001
系統(tǒng)中刪除key002:?1
判斷key002是否存在:false
設(shè)置?key001的過期時間為5秒:1
查看key001的剩余生存時間:3
移除key001的生存時間:1
查看key001的剩余生存時間:-1
查看key所儲存的值的類型:string
2)String功能
private?void?StringOperate()
{
System.out.println("======================String_1==========================");
//?清空數(shù)據(jù)
System.out.println("清空庫中所有數(shù)據(jù):"+jedis.flushDB());
System.out.println("=============增=============");
jedis.set("key001","value001");
jedis.set("key002","value002");
jedis.set("key003","value003");
System.out.println("已新增的3個鍵值對如下:");
System.out.println(jedis.get("key001"));
System.out.println(jedis.get("key002"));
System.out.println(jedis.get("key003"));
System.out.println("=============刪=============");
System.out.println("刪除key003鍵值對:"+jedis.del("key003"));
System.out.println("獲取key003鍵對應(yīng)的值:"+jedis.get("key003"));
System.out.println("=============改=============");????????//1、直接覆蓋原來的數(shù)據(jù)
System.out.println("直接覆蓋key001原來的數(shù)據(jù):"+jedis.set("key001","value001-update"));
System.out.println("獲取key001對應(yīng)的新值:"+jedis.get("key001"));????????//2、直接覆蓋原來的數(shù)據(jù)
System.out.println("在key002原來值后面追加:"+jedis.append("key002","+appendString"));
System.out.println("獲取key002對應(yīng)的新值"+jedis.get("key002"));
System.out.println("=============增,刪,查(多個)=============");????????/**
*?mset,mget同時新增,修改,查詢多個鍵值對
*?等價于:
*?jedis.set("name","ssss");
*?jedis.set("jarorwar","xxxx");
*/
System.out.println("一次性新增key201,key202,key203,key204及其對應(yīng)值:"+jedis.mset("key201","value201",????????????????????????"key202","value202","key203","value203","key204","value204"));
System.out.println("一次性獲取key201,key202,key203,key204各自對應(yīng)的值:"+
jedis.mget("key201","key202","key203","key204"));
System.out.println("一次性刪除key201,key202:"+jedis.del(new?String[]{"key201",?"key202"}));
System.out.println("一次性獲取key201,key202,key203,key204各自對應(yīng)的值:"+
jedis.mget("key201","key202","key203","key204"));
System.out.println();
//jedis具備的功能shardedJedis中也可直接使用,下面測試一些前面沒用過的方法
System.out.println("======================String_2==========================");
//?清空數(shù)據(jù)
System.out.println("清空庫中所有數(shù)據(jù):"+jedis.flushDB());
System.out.println("=============新增鍵值對時防止覆蓋原先值=============");
System.out.println("原先key301不存在時,新增key301:"+shardedJedis.setnx("key301",?"value301"));
System.out.println("原先key302不存在時,新增key302:"+shardedJedis.setnx("key302",?"value302"));
System.out.println("當key302存在時,嘗試新增key302:"+shardedJedis.setnx("key302",?"value302_new"));
System.out.println("獲取key301對應(yīng)的值:"+shardedJedis.get("key301"));
System.out.println("獲取key302對應(yīng)的值:"+shardedJedis.get("key302"));
System.out.println("=============超過有效期鍵值對被刪除=============");????????//?設(shè)置key的有效期,并存儲數(shù)據(jù)
System.out.println("新增key303,并指定過期時間為2秒"+shardedJedis.setex("key303",?2,?"key303-2second"));
System.out.println("獲取key303對應(yīng)的值:"+shardedJedis.get("key303"));
try{
Thread.sleep(3000);
}
catch?(InterruptedException?e){
}
System.out.println("3秒之后,獲取key303對應(yīng)的值:"+shardedJedis.get("key303"));
System.out.println("=============獲取原值,更新為新值一步完成=============");
System.out.println("key302原值:"+shardedJedis.getSet("key302",?"value302-after-getset"));
System.out.println("key302新值:"+shardedJedis.get("key302"));
System.out.println("=============獲取子串=============");
System.out.println("獲取key302對應(yīng)值中的子串:"+shardedJedis.getrange("key302",?5,?7));
}
運行結(jié)果:
======================String_1==========================
清空庫中所有數(shù)據(jù):OK
=============增=============
已新增的3個鍵值對如下:
value001
value002
value003
=============刪=============
刪除key003鍵值對:1
獲取key003鍵對應(yīng)的值:null
=============改=============
直接覆蓋key001原來的數(shù)據(jù):OK
獲取key001對應(yīng)的新值:value001-update
在key002原來值后面追加:21
獲取key002對應(yīng)的新值value002+appendString
=============增,刪,查(多個)=============
一次性新增key201,key202,key203,key204及其對應(yīng)值:OK
一次性獲取key201,key202,key203,key204各自對應(yīng)的值:[value201,?value202,?value203,?value204]
一次性刪除key201,key202:2
一次性獲取key201,key202,key203,key204各自對應(yīng)的值:[null,?null,?value203,?value204]
======================String_2==========================
清空庫中所有數(shù)據(jù):OK
=============新增鍵值對時防止覆蓋原先值=============
原先key301不存在時,新增key301:1
原先key302不存在時,新增key302:1
當key302存在時,嘗試新增key302:0
獲取key301對應(yīng)的值:value301
獲取key302對應(yīng)的值:value302
=============超過有效期鍵值對被刪除=============
新增key303,并指定過期時間為2秒OK
獲取key303對應(yīng)的值:key303-2second
3秒之后,獲取key303對應(yīng)的值:null
=============獲取原值,更新為新值一步完成=============
key302原值:value302
key302新值:value302-after-getset
=============獲取子串=============
獲取key302對應(yīng)值中的子串:302
3)List功能
private?void?ListOperate()
{
System.out.println("======================list==========================");
//?清空數(shù)據(jù)
System.out.println("清空庫中所有數(shù)據(jù):"+jedis.flushDB());
System.out.println("=============增=============");
shardedJedis.lpush("stringlists",?"vector");
shardedJedis.lpush("stringlists",?"ArrayList");
shardedJedis.lpush("stringlists",?"vector");
shardedJedis.lpush("stringlists",?"vector");
shardedJedis.lpush("stringlists",?"LinkedList");
shardedJedis.lpush("stringlists",?"MapList");
shardedJedis.lpush("stringlists",?"SerialList");
shardedJedis.lpush("stringlists",?"HashList");
shardedJedis.lpush("numberlists",?"3");
shardedJedis.lpush("numberlists",?"1");
shardedJedis.lpush("numberlists",?"5");
shardedJedis.lpush("numberlists",?"2");
System.out.println("所有元素-stringlists:"+shardedJedis.lrange("stringlists",?0,?-1));
System.out.println("所有元素-numberlists:"+shardedJedis.lrange("numberlists",?0,?-1));
System.out.println("=============刪=============");????????//?刪除列表指定的值?,第二個參數(shù)為刪除的個數(shù)(有重復(fù)時),后add進去的值先被刪,類似于出棧
System.out.println("成功刪除指定元素個數(shù)-stringlists:"+shardedJedis.lrem("stringlists",?2,?"vector"));
System.out.println("刪除指定元素之后-stringlists:"+shardedJedis.lrange("stringlists",?0,?-1));????????//?刪除區(qū)間以外的數(shù)據(jù)
System.out.println("刪除下標0-3區(qū)間之外的元素:"+shardedJedis.ltrim("stringlists",?0,?3));
System.out.println("刪除指定區(qū)間之外元素后-stringlists:"+shardedJedis.lrange("stringlists",?0,?-1));????????//?列表元素出棧
System.out.println("出棧元素:"+shardedJedis.lpop("stringlists"));
System.out.println("元素出棧后-stringlists:"+shardedJedis.lrange("stringlists",?0,?-1));
System.out.println("=============改=============");????????//?修改列表中指定下標的值
shardedJedis.lset("stringlists",?0,?"hello?list!");
System.out.println("下標為0的值修改后-stringlists:"+shardedJedis.lrange("stringlists",?0,?-1));
System.out.println("=============查=============");????????//?數(shù)組長度
System.out.println("長度-stringlists:"+shardedJedis.llen("stringlists"));
System.out.println("長度-numberlists:"+shardedJedis.llen("numberlists"));????????//?排序
/*
*?list中存字符串時必須指定參數(shù)為alpha,如果不使用SortingParams,而是直接使用sort("list"),
*?會出現(xiàn)"ERR?One?or?more?scores?can't?be?converted?into?double"?????????*/
SortingParams?sortingParameters?=?new?SortingParams();
sortingParameters.alpha();
sortingParameters.limit(0,?3);
System.out.println("返回排序后的結(jié)果-stringlists:"+shardedJedis.sort("stringlists",sortingParameters));
System.out.println("返回排序后的結(jié)果-numberlists:"+shardedJedis.sort("numberlists"));????????//?子串:??start為元素下標,end也為元素下標;-1代表倒數(shù)一個元素,-2代表倒數(shù)第二個元素
System.out.println("子串-第二個開始到結(jié)束:"+shardedJedis.lrange("stringlists",?1,?-1));????????//?獲取列表指定下標的值
System.out.println("獲取下標為2的元素:"+shardedJedis.lindex("stringlists",?2)+"\n");
}
運行結(jié)果:
======================list==========================
清空庫中所有數(shù)據(jù):OK
=============增=============
所有元素-stringlists:[HashList,?SerialList,?MapList,?LinkedList,?vector,?vector,?ArrayList,?vector]
所有元素-numberlists:[2,?5,?1,?3]
=============刪=============
成功刪除指定元素個數(shù)-stringlists:2
刪除指定元素之后-stringlists:[HashList,?SerialList,?MapList,?LinkedList,?ArrayList,?vector]
刪除下標0-3區(qū)間之外的元素:OK
刪除指定區(qū)間之外元素后-stringlists:[HashList,?SerialList,?MapList,?LinkedList]
出棧元素:HashList
元素出棧后-stringlists:[SerialList,?MapList,?LinkedList]
=============改=============
下標為0的值修改后-stringlists:[hello?list!,?MapList,?LinkedList]
=============查=============
長度-stringlists:3
長度-numberlists:4
返回排序后的結(jié)果-stringlists:[LinkedList,?MapList,?hello?list!]
返回排序后的結(jié)果-numberlists:[1,?2,?3,?5]
子串-第二個開始到結(jié)束:[MapList,?LinkedList]
獲取下標為2的元素:LinkedList
4)Set功能
private?void?SetOperate()
{
System.out.println("======================set==========================");
//?清空數(shù)據(jù)
System.out.println("清空庫中所有數(shù)據(jù):"+jedis.flushDB());
System.out.println("=============增=============");
System.out.println("向sets集合中加入元素element001:"+jedis.sadd("sets",?"element001"));
System.out.println("向sets集合中加入元素element002:"+jedis.sadd("sets",?"element002"));
System.out.println("向sets集合中加入元素element003:"+jedis.sadd("sets",?"element003"));
System.out.println("向sets集合中加入元素element004:"+jedis.sadd("sets",?"element004"));
System.out.println("查看sets集合中的所有元素:"+jedis.smembers("sets"));
System.out.println();
System.out.println("=============刪=============");
System.out.println("集合sets中刪除元素element003:"+jedis.srem("sets",?"element003"));
System.out.println("查看sets集合中的所有元素:"+jedis.smembers("sets"));????????/*System.out.println("sets集合中任意位置的元素出棧:"+jedis.spop("sets"));//注:出棧元素位置居然不定?--無實際意義
System.out.println("查看sets集合中的所有元素:"+jedis.smembers("sets"));*/
System.out.println();
System.out.println("=============改=============");
System.out.println();
System.out.println("=============查=============");
System.out.println("判斷element001是否在集合sets中:"+jedis.sismember("sets",?"element001"));
System.out.println("循環(huán)查詢獲取sets中的每個元素:");
Set?set?=?jedis.smembers("sets");
Iterator?it=set.iterator()?;
while(it.hasNext()){
Object?obj=it.next();
System.out.println(obj);
}
System.out.println();
System.out.println("=============集合運算=============");
System.out.println("sets1中添加元素element001:"+jedis.sadd("sets1",?"element001"));
System.out.println("sets1中添加元素element002:"+jedis.sadd("sets1",?"element002"));
System.out.println("sets1中添加元素element003:"+jedis.sadd("sets1",?"element003"));
System.out.println("sets1中添加元素element002:"+jedis.sadd("sets2",?"element002"));
System.out.println("sets1中添加元素element003:"+jedis.sadd("sets2",?"element003"));
System.out.println("sets1中添加元素element004:"+jedis.sadd("sets2",?"element004"));
System.out.println("查看sets1集合中的所有元素:"+jedis.smembers("sets1"));
System.out.println("查看sets2集合中的所有元素:"+jedis.smembers("sets2"));
System.out.println("sets1和sets2交集:"+jedis.sinter("sets1",?"sets2"));
System.out.println("sets1和sets2并集:"+jedis.sunion("sets1",?"sets2"));
System.out.println("sets1和sets2差集:"+jedis.sdiff("sets1",?"sets2"));//差集:set1中有,set2中沒有的元素
}
運行結(jié)果:
======================set==========================
清空庫中所有數(shù)據(jù):OK
=============增=============
向sets集合中加入元素element001:1
向sets集合中加入元素element002:1
向sets集合中加入元素element003:1
向sets集合中加入元素element004:1
查看sets集合中的所有元素:[element001,?element002,?element003,?element004]
=============刪=============
集合sets中刪除元素element003:1
查看sets集合中的所有元素:[element001,?element002,?element004]
=============改=============
=============查=============
判斷element001是否在集合sets中:true
循環(huán)查詢獲取sets中的每個元素:
element001
element002
element004
=============集合運算=============
sets1中添加元素element001:1
sets1中添加元素element002:1
sets1中添加元素element003:1
sets1中添加元素element002:1
sets1中添加元素element003:1
sets1中添加元素element004:1
查看sets1集合中的所有元素:[element001,?element002,?element003]
查看sets2集合中的所有元素:[element002,?element003,?element004]
sets1和sets2交集:[element002,?element003]
sets1和sets2并集:[element001,?element002,?element003,?element004]
sets1和sets2差集:[element001]
5)SortedSet功能(有序集合)
private?void?SortedSetOperate()
{
System.out.println("======================zset==========================");
//?清空數(shù)據(jù)?????????System.out.println(jedis.flushDB());
System.out.println("=============增=============");
System.out.println("zset中添加元素element001:"+shardedJedis.zadd("zset",?7.0,?"element001"));
System.out.println("zset中添加元素element002:"+shardedJedis.zadd("zset",?8.0,?"element002"));
System.out.println("zset中添加元素element003:"+shardedJedis.zadd("zset",?2.0,?"element003"));
System.out.println("zset中添加元素element004:"+shardedJedis.zadd("zset",?3.0,?"element004"));
System.out.println("zset集合中的所有元素:"+shardedJedis.zrange("zset",?0,?-1));//按照權(quán)重值排序????????System.out.println();
System.out.println("=============刪=============");
System.out.println("zset中刪除元素element002:"+shardedJedis.zrem("zset",?"element002"));
System.out.println("zset集合中的所有元素:"+shardedJedis.zrange("zset",?0,?-1));
System.out.println();
System.out.println("=============改=============");
System.out.println();
System.out.println("=============查=============");
System.out.println("統(tǒng)計zset集合中的元素中個數(shù):"+shardedJedis.zcard("zset"));
System.out.println("統(tǒng)計zset集合中權(quán)重某個范圍內(nèi)(1.0——5.0),元素的個數(shù):"+shardedJedis.zcount("zset",?1.0,?5.0));
System.out.println("查看zset集合中element004的權(quán)重:"+shardedJedis.zscore("zset",?"element004"));
System.out.println("查看下標1到2范圍內(nèi)的元素值:"+shardedJedis.zrange("zset",?1,?2));
}
運行結(jié)果:
======================zset==========================
OK
=============增=============
zset中添加元素element001:1
zset中添加元素element002:1
zset中添加元素element003:1
zset中添加元素element004:1
zset集合中的所有元素:[element003,?element004,?element001,?element002]
=============刪=============
zset中刪除元素element002:1
zset集合中的所有元素:[element003,?element004,?element001]
=============改=============
=============查=============
統(tǒng)計zset集合中的元素中個數(shù):3
統(tǒng)計zset集合中權(quán)重某個范圍內(nèi)(1.0——5.0),元素的個數(shù):2
查看zset集合中element004的權(quán)重:3.0
查看下標1到2范圍內(nèi)的元素值:[element004,?element001]
6)Hash功能
private?void?HashOperate()
{
System.out.println("======================hash==========================");????????//清空數(shù)據(jù)?????????System.out.println(jedis.flushDB());
System.out.println("=============增=============");
System.out.println("hashs中添加key001和value001鍵值對:"+shardedJedis.hset("hashs",?"key001",?"value001"));
System.out.println("hashs中添加key002和value002鍵值對:"+shardedJedis.hset("hashs",?"key002",?"value002"));
System.out.println("hashs中添加key003和value003鍵值對:"+shardedJedis.hset("hashs",?"key003",?"value003"));
System.out.println("新增key004和4的整型鍵值對:"+shardedJedis.hincrBy("hashs",?"key004",?4l));
System.out.println("hashs中的所有值:"+shardedJedis.hvals("hashs"));
System.out.println();
System.out.println("=============刪=============");
System.out.println("hashs中刪除key002鍵值對:"+shardedJedis.hdel("hashs",?"key002"));
System.out.println("hashs中的所有值:"+shardedJedis.hvals("hashs"));
System.out.println();
System.out.println("=============改=============");
System.out.println("key004整型鍵值的值增加100:"+shardedJedis.hincrBy("hashs",?"key004",?100l));
System.out.println("hashs中的所有值:"+shardedJedis.hvals("hashs"));
System.out.println();
System.out.println("=============查=============");
System.out.println("判斷key003是否存在:"+shardedJedis.hexists("hashs",?"key003"));
System.out.println("獲取key004對應(yīng)的值:"+shardedJedis.hget("hashs",?"key004"));
System.out.println("批量獲取key001和key003對應(yīng)的值:"+shardedJedis.hmget("hashs",?"key001",?"key003"));
System.out.println("獲取hashs中所有的key:"+shardedJedis.hkeys("hashs"));
System.out.println("獲取hashs中所有的value:"+shardedJedis.hvals("hashs"));
System.out.println();
}
運行結(jié)果:
======================hash==========================
OK
=============增=============
hashs中添加key001和value001鍵值對:1
hashs中添加key002和value002鍵值對:1
hashs中添加key003和value003鍵值對:1
新增key004和4的整型鍵值對:4
hashs中的所有值:[value001,?value002,?value003,?4]
=============刪=============
hashs中刪除key002鍵值對:1
hashs中的所有值:[value001,?value003,?4]
=============改=============
key004整型鍵值的值增加100:104
hashs中的所有值:[value001,?value003,?104]
=============查=============
判斷key003是否存在:true
獲取key004對應(yīng)的值:104
批量獲取key001和key003對應(yīng)的值:[value001,?value003]
獲取hashs中所有的key:[key004,?key003,?key001]
獲取hashs中所有的value:[value001,?value003,?104]