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. }  

转载于:https://my.oschina.net/airship/blog/1611884

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值