Java中使用Jedis连接Redis对Hash进行操作的常用命令

场景

Centos 中 Redis 的下载编译与安装 (超详细):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103967334

Redis 的启动和关闭 (前台启动和后台启动):

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103972348

RedisDesktopManager 客户端可视化工具下载安装与使用:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103983147

Java 中使用 Jedis 连接 Redis 对 Key 进行操作的常用命令:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/104915569

参考上面对 Key 进行操作的命令后,下面是对 Hash 进行常用操作。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

新建 Junit 方法 HashTest,然后依次打开以下注释代码

    @Test
     public void HashTest()
     {
         //Map<String,String> map = new HashMap<String,String>();
         //map.put("key1","value1");
         //map.put("key2","value2");
         //map.put("key3","value3");
         //添加名称为 hash 的 hash 元素
         //jedis.hmset("hash",map);
     </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">向名称为hash的hash中添加元素
     </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">jedis.hset("hash","key5","value5");
     </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash"));
     </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println("散列hash的所有键为:"+jedis.hkeys("hash"));
     </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println("散列hash的所有值为:"+jedis.hvals("hash"));
     </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println("将key6保存的值加上一个整数,如果keys不存在则添加key6:"+jedis.hincrBy("hash","key6",3));
     </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">System.out.println("删除一个或者多个键值对:"+jedis.hdel("hash","key2"));</span>
     System.<span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">散列hash的键值对的个数为:</span><span style="color: rgba(128, 0, 0, 1)">"</span>+jedis.hlen(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">hash</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">));
     System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">判断hash中是否存在key2:</span><span style="color: rgba(128, 0, 0, 1)">"</span>+jedis.hexists(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">hash</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">key2</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">));
 } </span></pre>