Redis 入門之 redis 對hash的操做

  1. /** 
  2.  *  {@link #test() test} 
  3.  * jedis 對 hash 進行操做 
  4.  * @author jackson 
  5.  * @date 2015-12-17 下午2:48:30 
  6.  * @return void 
  7.  */  
  8. @SuppressWarnings("unchecked")  
  9. @Test  
  10. public void TestJedisHash(){  
  11.     // hset  hget  
  12.     jedis.hset("hsetkey", "hashKey", "hashValue");//將哈希表key 中的域field 的值設爲value 。若是key 不存在,一個新的哈希表被建立並進行HSET 操做。若是域field 已經存在於哈希表中,舊值將被覆蓋。  
  13.     String hash = jedis.hget("hsetkey", "hashKey");//返回哈希表key 中給定域field 的值  
  14.     System.out.println("測試 hset hget : hsetkey 的返回值:"+hash);  
  15.       
  16.     //hsetnx  當且僅當域field 不存在。若域field(指第二個參數) 已經存在,該操做無效。   
  17.     long n = jedis.hsetnx("hsetkeynx", "hashkeynx", "hashvaluenx");  
  18.     System.out.println(n!=0?"操做成功":"操做失敗");  
  19.     n = jedis.hsetnx("hsetkeynx", "hashkey", "hashvaluenx");  
  20.     System.out.println(n!=0?"操做成功":"操做失敗");  
  21.     n = jedis.hsetnx("hsetkeynx", "hashkey", "hashvaluenx");  
  22.     System.out.println(n!=0?"操做成功":"操做失敗");  
  23.       
  24.     //hmset hmget  
  25.     HashMap<String, String> hashMap = new HashMap<String, String>();  
  26.     hashMap.put("hashMap1", "hashValue1");  
  27.     hashMap.put("hashMap2", "hashValue2");  
  28.     hashMap.put("hashMap3", "hashValue3");  
  29.     hashMap.put("hashMap4", "hashValue4");  
  30.     String status  = jedis.hmset("hashMapkey", hashMap);//若是命令執行成功,返回OK 。當key 不是哈希表(hash) 類型時,返回一個錯誤。  
  31.     hash = jedis.hget("hashMapkey", "hashMap4");  
  32.     System.out.println("OK".equals(status)?"操做成功  返回值:"+hash:"操做失敗");  
  33.     //返回值: 一個包含多個給定域的關聯值的表,表值的排列順序和給定域參數的請求順序同樣  
  34.     List<String> hashList = jedis.hmget("hashMapkey", "hashMap1 hashMap2 hashMap3 hashMap4".split(" "));  
  35.     for(String value : hashList){  
  36.         System.out.print("對應的value值:  "+value+" ");//返回值: 一個包含多個給定域的關聯值的表,表值的排列順序和給定域參數的請求順序同樣  
  37.     }  
  38.     System.out.println();  
  39.       
  40.     //hgetall  得到一個Map 返回key整個file域  
  41.     Map<String,String> hashMapKey = jedis.hgetAll("hashMapkey");  
  42.       
  43.     // map 的第一種迭代方式  
  44.     Set<Map.Entry<String, String>> entry = hashMapKey.entrySet();  
  45.     Iterator<Map.Entry<String, String>> it = entry.iterator();  
  46.     while(it.hasNext()){  
  47.         Map.Entry<String, String> e  = it.next();  
  48.         System.out.println("key: "+e.getKey()+"  value: "+e.getValue());  
  49.     }  
  50.       
  51.     // map的第二種迭代方式  
  52.     Set<String> keySet = hashMapKey.keySet();// map中的全部key在set中存放着,能夠經過迭代set的方式 來得到key  
  53.     Iterator<String> iter = keySet.iterator();  
  54.     while(iter.hasNext()){  
  55.         String key = iter.next();  
  56.         String value = hashMapKey.get(key);  
  57.     }  
  58.       
  59.   
  60.     //hscan  相似於 scan 遍歷庫中 key 下全部的域   返回  file-value 以map 的形式;  
  61.     ScanResult<Map.Entry<String, String>> hscanResult = jedis.hscan("hashMapkey", "0");  
  62.     String cursor = hscanResult.getCursor(); // 返回0 說明遍歷完成  
  63.     System.out.println("遊標"+cursor);  
  64.     List<Map.Entry<String, String>> scanResult = hscanResult.getResult();  
  65.     for(int m = 0;m < scanResult.size();m++){  
  66.         Map.Entry<String, String> mapentry  = scanResult.get(m);  
  67.         System.out.println("key: "+mapentry.getKey()+"  value: "+mapentry.getValue());  
  68.     }  
  69.       
  70.     //hkeys  
  71.     Set<String> setKey = jedis.hkeys("hashMapkey");// keys 返回 全部的key  ,hkeys 返回 key 下面的全部的 域  
  72.     Iterator<String> itset = setKey.iterator();  
  73.     String files = "";  
  74.     while(itset.hasNext()){  
  75.         files =files+" "+itset.next();  
  76.     }  
  77.     System.out.println("hashMapkey 中的全部域 爲:"+files);  
  78.       
  79.     //hvals 返回哈希表key 中全部域的值。可用版本: >= 2.0.0時間複雜度: O(N),N 爲哈希表的大小。返回值:一個包含哈希表中全部值的表。當key 不存在時,返回一個空表。  
  80.     List<String> list = jedis.hvals("hashMapkey");  
  81.     for(String s : list){  
  82.         System.out.println(s);  
  83.     }  
  84.       
  85.     // 以上 域對應的值是String  下面域對應的值 是list  
  86.     Map<String,List<String>> testMapList = new HashMap<String,List<String>>();  
  87.     List<String> testList = Arrays.asList("testList testList testList testList testList testList testList ");  
  88.     List<String> testList1 = Arrays.asList("testList1 testList1 testList1 testList1 testList1 testList1 testList1 ");  
  89.     List<String> testList2 = Arrays.asList("testList2 testList2 testList2 testList2 testList2 testList2 testList2 ");  
  90.     testMapList.put("testList", testList);  
  91.     testMapList.put("testList1", testList1);  
  92.     testMapList.put("testList2", testList2);  
  93.     String mapString  =  JSON.toJSONString(testMapList,true);// map 轉爲json串  
  94.     jedis.set("hashMapkey2", mapString);  
  95.     mapString = jedis.get("hashMapkey2");  
  96. /       System.out.println(mapString);    
  97.     testMapList = (Map<String,List<String>>)JSON.parse(mapString);  
  98.     Set<Map.Entry<String, List<String>>> mapListSet = testMapList.entrySet();  
  99.     Iterator<Map.Entry<String, List<String>>> maplistIter = mapListSet.iterator();  
  100.     while(maplistIter.hasNext()){  
  101.         Map.Entry<String, List<String>> mapentryList = maplistIter.next();  
  102.         String key = mapentryList.getKey();  
  103.         List<String> entryList = mapentryList.getValue();  
  104.         System.out.println("testMapList key: "+key+"testMapList value: "+entryList.toString());  
  105.     }  
  106.     // Map 裏面存儲實體對象  
  107.     Map<String,Bar> testMapEntity = new HashMap<String,Bar>();  
  108.     Bar bar = new Bar();bar.setColor("red");bar.setName("lvxiaojian");  
  109.     Bar bar1 = new Bar();bar.setColor("green");bar.setName("wagnbo");  
  110.     testMapEntity.put("bar", bar);  
  111.     testMapEntity.put("bar1", bar1);  
  112.     String entityString  =  JSON.toJSONString(testMapEntity,true);// map 轉爲json串  
  113.     jedis.set("hashMapkey3", entityString);  
  114.     entityString = jedis.get("hashMapkey3");  
  115.     testMapEntity = (Map<String,Bar>)JSON.parse(entityString);  
  116.     Set<String> entitySet = testMapEntity.keySet();  
  117.     Iterator<String> iterentity = entitySet.iterator();  
  118.     while(iterentity.hasNext()){  
  119.         System.out.println("testMapEntity key: "+iterentity.next()+"testMapEntity value: "+testMapEntity.get(iterentity.next()));  
  120.     }  
  121.       
  122.       
  123.     //hlen  返回值:哈希表中域的數量。當key 不存在時,返回0 。  
  124.     n = jedis.hlen("hashMapkey");  
  125.     System.out.println("hashMapkey 中域的數量爲: "+n);  
  126.       
  127.     //hdel  返回值: 被成功移除的域的數量,不包括被忽略的域  
  128.     n = jedis.hdel("hashMapkey","hashMap1 hashMap2 hashMap3 hashMap4".split(" "));  
  129.     System.out.println("被成功移除的域的數量,不包括被忽略的域: "+n);  
  130.       
  131.     //hexists  返回值:若是哈希表含有給定域,返回1 。若是哈希表不含有給定域,或key 不存在,返回0 。  
  132.     boolean flag = jedis.hexists("hashMapkey", "hashMap1");  
  133.     System.out.println(flag?"哈希表含有給定域":"哈希表不含有給定域");  
  134.       
  135.     hashMap.clear();// 清除map  
  136.     hashMap.put("hashMap1", "1");  
  137.     hashMap.put("hashMap2", "2");  
  138.     hashMap.put("hashMap3", "3");  
  139.     hashMap.put("hashMap4", "4");  
  140.     hashMap.put("hashMap5", "5");  
  141.     hashMap.put("hashMap6", "6");  
  142.     jedis.hmset("hashMapkey", hashMap);  
  143.     flag = jedis.hexists("hashMapkey", "hashMap1");  
  144.     System.out.println(flag?"哈希表含有給定域":"哈希表不含有給定域");  
  145.       
  146.     //hincrBy  key 存在  域也存在的狀況  返回值: 執行HINCRBY 命令以後,哈希表key 中域field 的值  
  147.     System.out.println("對 hash表中key 爲hashMapkey 的域hashMap1 的值   減去 1 以前數據爲:"+jedis.hget("hashMapkey", "hashMap1"));// 返回值:對 hash表中key 爲hashMapkey 的域hashMap1 的值   減去 1 以前數據爲:1  
  148.     n = jedis.hincrBy("hashMapkey", "hashMap1", -1); // 對 hash表中key 爲hashMapkey 的域hashMap1 的值  減去 1  
  149.     System.out.println("對 hash表中key 爲hashMapkey 的域hashMap1 的值  減去 1 結果爲:"+n);// 返回值:對 hash表中key 爲hashMapkey 的域hashMap1 的值  減去 1 結果爲:0  
  150.       
  151.     System.out.println("對 hash表中key 爲hashMapkey 的域hashMap2 的值  加上 2 以前數據爲:"+jedis.hget("hashMapkey", "hashMap2"));//返回值:對 hash表中key 爲hashMapkey 的域hashMap2 的值  加上 2 以前數據爲:2  
  152.     n = jedis.hincrBy("hashMapkey", "hashMap2", 2); // 對 hash表中key 爲hashMapkey 的域hashMap2 的值  加上 2  
  153.     System.out.println("對 hash表中key 爲hashMapkey 的域hashMap2 的值  加上 2 結果爲:"+n);//返回值:對 hash表中key 爲hashMapkey 的域hashMap2 的值  加上 2 結果爲:4  
  154.       
  155.     // key 存在  域不存在的狀況  作加 減 操做:從如下操做能夠看出,當域不存在的時候,在執行操做的時候會先給域的值默認初始化爲 0 在進行加減操做  
  156.     System.out.println("對 hash表中key 爲hashMapkey 的域hashMap7 的值   減去 1 以前數據爲:"+jedis.hget("hashMapkey", "hashMap7"));//返回值:對 hash表中key 爲hashMapkey 的域hashMap7 的值   減去 1 以前數據爲:null  
  157.     n = jedis.hincrBy("hashMapkey", "hashMap7", -1); // 對 hash表中key 爲hashMapkey 的域hashMap1 的值  減去 1  
  158.     System.out.println("對 hash表中key 爲hashMapkey 的域hashMap7的值  減去 1 結果爲:"+n);//返回值:對 hash表中key 爲hashMapkey 的域hashMap7的值  減去 1 結果爲:-1  
  159.       
  160.     System.out.println("對 hash表中key 爲hashMapkey 的域hashMap8 的值  加上 2 以前數據爲:"+jedis.hget("hashMapkey", "hashMap8"));//返回值:對 hash表中key 爲hashMapkey 的域hashMap8 的值  加上 2 以前數據爲:null  
  161.     n = jedis.hincrBy("hashMapkey", "hashMap8", 2); // 對 hash表中key 爲hashMapkey 的域hashMap2 的值  加上 2  
  162.     System.out.println("對 hash表中key 爲hashMapkey 的域hashMap8 的值  加上 2 結果爲:"+n);//對 hash表中key 爲hashMapkey 的域hashMap8 的值  加上 2 結果爲:2  
  163.       
  164.     //key 不存在 執行操做 前先給 個默認值 初始 爲 0  先執行set 操做,在執行 hincrby 操做  
  165.     System.out.println("對 hash表中key 爲hashMapkey1 的域hashMap7 的值   減去 1 以前數據爲:"+jedis.hget("hashMapkey1", "hashMap7"));//返回值:對 hash表中key 爲hashMapkey 的域hashMap7 的值   減去 1 以前數據爲:null  
  166.     n = jedis.hincrBy("hashMapkey1", "hashMap7", -1); // 對 hash表中key 爲hashMapkey 的域hashMap1 的值  減去 1  
  167.     System.out.println("對 hash表中key 爲hashMapkey 的域hashMap7的值  減去 1 結果爲:"+n);//返回值:對 hash表中key 爲hashMapkey 的域hashMap7的值  減去 1 結果爲:-1  
  168.       
  169.     System.out.println("對 hash表中key 爲hashMapkey 的域hashMap8 的值  加上 2 以前數據爲:"+jedis.hget("hashMapkey1", "hashMap8"));//返回值:對 hash表中key 爲hashMapkey 的域hashMap8 的值  加上 2 以前數據爲:null  
  170.     n = jedis.hincrBy("hashMapkey1", "hashMap8", 2); // 對 hash表中key 爲hashMapkey 的域hashMap2 的值  加上 2  
  171.     System.out.println("對 hash表中key 爲hashMapkey1 的域hashMap8 的值  加上 2 結果爲:"+n);//對 hash表中key 爲hashMapkey 的域hashMap8 的值  加上 2 結果爲:2  
  172.       
  173.     //incrbyfloat 操做相似於 incrby 不過這個返回值是double 精度更高  
  174. }  
相關文章
相關標籤/搜索