Redis 入門之 redis 對hash的操做
- /**
- * {@link #test() test}
- * jedis 對 hash 進行操做
- * @author jackson
- * @date 2015-12-17 下午2:48:30
- * @return void
- */
- @SuppressWarnings("unchecked")
- @Test
- public void TestJedisHash(){
- // hset hget
- jedis.hset("hsetkey", "hashKey", "hashValue");//將哈希表key 中的域field 的值設爲value 。若是key 不存在,一個新的哈希表被建立並進行HSET 操做。若是域field 已經存在於哈希表中,舊值將被覆蓋。
- String hash = jedis.hget("hsetkey", "hashKey");//返回哈希表key 中給定域field 的值
- System.out.println("測試 hset hget : hsetkey 的返回值:"+hash);
-
- //hsetnx 當且僅當域field 不存在。若域field(指第二個參數) 已經存在,該操做無效。
- long n = jedis.hsetnx("hsetkeynx", "hashkeynx", "hashvaluenx");
- System.out.println(n!=0?"操做成功":"操做失敗");
- n = jedis.hsetnx("hsetkeynx", "hashkey", "hashvaluenx");
- System.out.println(n!=0?"操做成功":"操做失敗");
- n = jedis.hsetnx("hsetkeynx", "hashkey", "hashvaluenx");
- System.out.println(n!=0?"操做成功":"操做失敗");
-
- //hmset hmget
- HashMap<String, String> hashMap = new HashMap<String, String>();
- hashMap.put("hashMap1", "hashValue1");
- hashMap.put("hashMap2", "hashValue2");
- hashMap.put("hashMap3", "hashValue3");
- hashMap.put("hashMap4", "hashValue4");
- String status = jedis.hmset("hashMapkey", hashMap);//若是命令執行成功,返回OK 。當key 不是哈希表(hash) 類型時,返回一個錯誤。
- hash = jedis.hget("hashMapkey", "hashMap4");
- System.out.println("OK".equals(status)?"操做成功 返回值:"+hash:"操做失敗");
- //返回值: 一個包含多個給定域的關聯值的表,表值的排列順序和給定域參數的請求順序同樣
- List<String> hashList = jedis.hmget("hashMapkey", "hashMap1 hashMap2 hashMap3 hashMap4".split(" "));
- for(String value : hashList){
- System.out.print("對應的value值: "+value+" ");//返回值: 一個包含多個給定域的關聯值的表,表值的排列順序和給定域參數的請求順序同樣
- }
- System.out.println();
-
- //hgetall 得到一個Map 返回key整個file域
- Map<String,String> hashMapKey = jedis.hgetAll("hashMapkey");
-
- // map 的第一種迭代方式
- Set<Map.Entry<String, String>> entry = hashMapKey.entrySet();
- Iterator<Map.Entry<String, String>> it = entry.iterator();
- while(it.hasNext()){
- Map.Entry<String, String> e = it.next();
- System.out.println("key: "+e.getKey()+" value: "+e.getValue());
- }
-
- // map的第二種迭代方式
- Set<String> keySet = hashMapKey.keySet();// map中的全部key在set中存放着,能夠經過迭代set的方式 來得到key
- Iterator<String> iter = keySet.iterator();
- while(iter.hasNext()){
- String key = iter.next();
- String value = hashMapKey.get(key);
- }
-
-
- //hscan 相似於 scan 遍歷庫中 key 下全部的域 返回 file-value 以map 的形式;
- ScanResult<Map.Entry<String, String>> hscanResult = jedis.hscan("hashMapkey", "0");
- String cursor = hscanResult.getCursor(); // 返回0 說明遍歷完成
- System.out.println("遊標"+cursor);
- List<Map.Entry<String, String>> scanResult = hscanResult.getResult();
- for(int m = 0;m < scanResult.size();m++){
- Map.Entry<String, String> mapentry = scanResult.get(m);
- System.out.println("key: "+mapentry.getKey()+" value: "+mapentry.getValue());
- }
-
- //hkeys
- Set<String> setKey = jedis.hkeys("hashMapkey");// keys 返回 全部的key ,hkeys 返回 key 下面的全部的 域
- Iterator<String> itset = setKey.iterator();
- String files = "";
- while(itset.hasNext()){
- files =files+" "+itset.next();
- }
- System.out.println("hashMapkey 中的全部域 爲:"+files);
-
- //hvals 返回哈希表key 中全部域的值。可用版本: >= 2.0.0時間複雜度: O(N),N 爲哈希表的大小。返回值:一個包含哈希表中全部值的表。當key 不存在時,返回一個空表。
- List<String> list = jedis.hvals("hashMapkey");
- for(String s : list){
- System.out.println(s);
- }
-
- // 以上 域對應的值是String 下面域對應的值 是list
- Map<String,List<String>> testMapList = new HashMap<String,List<String>>();
- List<String> testList = Arrays.asList("testList testList testList testList testList testList testList ");
- List<String> testList1 = Arrays.asList("testList1 testList1 testList1 testList1 testList1 testList1 testList1 ");
- List<String> testList2 = Arrays.asList("testList2 testList2 testList2 testList2 testList2 testList2 testList2 ");
- testMapList.put("testList", testList);
- testMapList.put("testList1", testList1);
- testMapList.put("testList2", testList2);
- String mapString = JSON.toJSONString(testMapList,true);// map 轉爲json串
- jedis.set("hashMapkey2", mapString);
- mapString = jedis.get("hashMapkey2");
- / System.out.println(mapString);
- testMapList = (Map<String,List<String>>)JSON.parse(mapString);
- Set<Map.Entry<String, List<String>>> mapListSet = testMapList.entrySet();
- Iterator<Map.Entry<String, List<String>>> maplistIter = mapListSet.iterator();
- while(maplistIter.hasNext()){
- Map.Entry<String, List<String>> mapentryList = maplistIter.next();
- String key = mapentryList.getKey();
- List<String> entryList = mapentryList.getValue();
- System.out.println("testMapList key: "+key+"testMapList value: "+entryList.toString());
- }
- // Map 裏面存儲實體對象
- Map<String,Bar> testMapEntity = new HashMap<String,Bar>();
- Bar bar = new Bar();bar.setColor("red");bar.setName("lvxiaojian");
- Bar bar1 = new Bar();bar.setColor("green");bar.setName("wagnbo");
- testMapEntity.put("bar", bar);
- testMapEntity.put("bar1", bar1);
- String entityString = JSON.toJSONString(testMapEntity,true);// map 轉爲json串
- jedis.set("hashMapkey3", entityString);
- entityString = jedis.get("hashMapkey3");
- testMapEntity = (Map<String,Bar>)JSON.parse(entityString);
- Set<String> entitySet = testMapEntity.keySet();
- Iterator<String> iterentity = entitySet.iterator();
- while(iterentity.hasNext()){
- System.out.println("testMapEntity key: "+iterentity.next()+"testMapEntity value: "+testMapEntity.get(iterentity.next()));
- }
-
-
- //hlen 返回值:哈希表中域的數量。當key 不存在時,返回0 。
- n = jedis.hlen("hashMapkey");
- System.out.println("hashMapkey 中域的數量爲: "+n);
-
- //hdel 返回值: 被成功移除的域的數量,不包括被忽略的域
- n = jedis.hdel("hashMapkey","hashMap1 hashMap2 hashMap3 hashMap4".split(" "));
- System.out.println("被成功移除的域的數量,不包括被忽略的域: "+n);
-
- //hexists 返回值:若是哈希表含有給定域,返回1 。若是哈希表不含有給定域,或key 不存在,返回0 。
- boolean flag = jedis.hexists("hashMapkey", "hashMap1");
- System.out.println(flag?"哈希表含有給定域":"哈希表不含有給定域");
-
- hashMap.clear();// 清除map
- hashMap.put("hashMap1", "1");
- hashMap.put("hashMap2", "2");
- hashMap.put("hashMap3", "3");
- hashMap.put("hashMap4", "4");
- hashMap.put("hashMap5", "5");
- hashMap.put("hashMap6", "6");
- jedis.hmset("hashMapkey", hashMap);
- flag = jedis.hexists("hashMapkey", "hashMap1");
- System.out.println(flag?"哈希表含有給定域":"哈希表不含有給定域");
-
- //hincrBy key 存在 域也存在的狀況 返回值: 執行HINCRBY 命令以後,哈希表key 中域field 的值
- System.out.println("對 hash表中key 爲hashMapkey 的域hashMap1 的值 減去 1 以前數據爲:"+jedis.hget("hashMapkey", "hashMap1"));// 返回值:對 hash表中key 爲hashMapkey 的域hashMap1 的值 減去 1 以前數據爲:1
- n = jedis.hincrBy("hashMapkey", "hashMap1", -1); // 對 hash表中key 爲hashMapkey 的域hashMap1 的值 減去 1
- System.out.println("對 hash表中key 爲hashMapkey 的域hashMap1 的值 減去 1 結果爲:"+n);// 返回值:對 hash表中key 爲hashMapkey 的域hashMap1 的值 減去 1 結果爲:0
-
- System.out.println("對 hash表中key 爲hashMapkey 的域hashMap2 的值 加上 2 以前數據爲:"+jedis.hget("hashMapkey", "hashMap2"));//返回值:對 hash表中key 爲hashMapkey 的域hashMap2 的值 加上 2 以前數據爲:2
- n = jedis.hincrBy("hashMapkey", "hashMap2", 2); // 對 hash表中key 爲hashMapkey 的域hashMap2 的值 加上 2
- System.out.println("對 hash表中key 爲hashMapkey 的域hashMap2 的值 加上 2 結果爲:"+n);//返回值:對 hash表中key 爲hashMapkey 的域hashMap2 的值 加上 2 結果爲:4
-
- // key 存在 域不存在的狀況 作加 減 操做:從如下操做能夠看出,當域不存在的時候,在執行操做的時候會先給域的值默認初始化爲 0 在進行加減操做
- System.out.println("對 hash表中key 爲hashMapkey 的域hashMap7 的值 減去 1 以前數據爲:"+jedis.hget("hashMapkey", "hashMap7"));//返回值:對 hash表中key 爲hashMapkey 的域hashMap7 的值 減去 1 以前數據爲:null
- n = jedis.hincrBy("hashMapkey", "hashMap7", -1); // 對 hash表中key 爲hashMapkey 的域hashMap1 的值 減去 1
- System.out.println("對 hash表中key 爲hashMapkey 的域hashMap7的值 減去 1 結果爲:"+n);//返回值:對 hash表中key 爲hashMapkey 的域hashMap7的值 減去 1 結果爲:-1
-
- System.out.println("對 hash表中key 爲hashMapkey 的域hashMap8 的值 加上 2 以前數據爲:"+jedis.hget("hashMapkey", "hashMap8"));//返回值:對 hash表中key 爲hashMapkey 的域hashMap8 的值 加上 2 以前數據爲:null
- n = jedis.hincrBy("hashMapkey", "hashMap8", 2); // 對 hash表中key 爲hashMapkey 的域hashMap2 的值 加上 2
- System.out.println("對 hash表中key 爲hashMapkey 的域hashMap8 的值 加上 2 結果爲:"+n);//對 hash表中key 爲hashMapkey 的域hashMap8 的值 加上 2 結果爲:2
-
- //key 不存在 執行操做 前先給 個默認值 初始 爲 0 先執行set 操做,在執行 hincrby 操做
- System.out.println("對 hash表中key 爲hashMapkey1 的域hashMap7 的值 減去 1 以前數據爲:"+jedis.hget("hashMapkey1", "hashMap7"));//返回值:對 hash表中key 爲hashMapkey 的域hashMap7 的值 減去 1 以前數據爲:null
- n = jedis.hincrBy("hashMapkey1", "hashMap7", -1); // 對 hash表中key 爲hashMapkey 的域hashMap1 的值 減去 1
- System.out.println("對 hash表中key 爲hashMapkey 的域hashMap7的值 減去 1 結果爲:"+n);//返回值:對 hash表中key 爲hashMapkey 的域hashMap7的值 減去 1 結果爲:-1
-
- System.out.println("對 hash表中key 爲hashMapkey 的域hashMap8 的值 加上 2 以前數據爲:"+jedis.hget("hashMapkey1", "hashMap8"));//返回值:對 hash表中key 爲hashMapkey 的域hashMap8 的值 加上 2 以前數據爲:null
- n = jedis.hincrBy("hashMapkey1", "hashMap8", 2); // 對 hash表中key 爲hashMapkey 的域hashMap2 的值 加上 2
- System.out.println("對 hash表中key 爲hashMapkey1 的域hashMap8 的值 加上 2 結果爲:"+n);//對 hash表中key 爲hashMapkey 的域hashMap8 的值 加上 2 結果爲:2
-
- //incrbyfloat 操做相似於 incrby 不過這個返回值是double 精度更高
- }
歡迎關注本站公眾號,獲取更多信息