Redis的Hash操作

介绍

关于SpringBoot整合Jedis操作Redis,可参考

SpringBoot整合Jedis 二

在开发中,我们经常会用到Redis存储hash数据,在redis中hash的结构为

key map<key, value>

即一个key对应一个map,map中有多对的key-value键值对

下面我们使用redis desktop manager来进行命令行操作

设置一个hash

hset key field value

结构说明:
hset key map
map = <field, value>

注:hmset key field value [field value…]
可以参考下面代码

Jedis操作Redis的Hash代码


    /*########################  hash(哈希表)的操作  #######################*/
    //hset hget hmset hmget hgetall hdel hkeys hvals hexists hincrby

    /**
     * 给某个hash表设置一个键值对
     *
     * @param key
     * @param field
     * @param value
     */
    public  void hset(String key, String field, String value) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.hset(key, field, value);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }

    /**
     * 取出某个hash表中某个field对应的value
     *
     * @param key key
     * @param field field
     * @return
     */
    public  String hget(String key, String field) {
        Jedis jedis = null;
        String hget = null;
        try {
            jedis = getJedis();
            hget = jedis.hget(key, field);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return hget;
    }

    /**
     * 某个hash表设置一个或多个键值对
     *
     * @param key
     * @param kvMap
     */
    public  void hmset(String key, Map<String, String> kvMap) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.hmset(key, kvMap);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }

    /**
     * 取出某个hash表中任意多个key对应的value的集合
     *
     * @param key
     * @param fields
     * @return
     */
    public  List<String> hmget(String key, String... fields) {
        Jedis jedis = null;
        List<String> hmget = null;
        try {
            jedis = getJedis();
            hmget = jedis.hmget(key, fields);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return hmget;
    }

    /**
     * 取出某个hash表中所有的键值对
     *
     * @param key
     * @return
     */
    public  Map<String, String> hgetall(String key) {
        Jedis jedis = null;
        Map<String, String> kvMap = null;
        try {
            jedis = getJedis();
            kvMap = jedis.hgetAll(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return kvMap;
    }

    /**
     * 判断某个hash表中的某个key是否存在
     *
     * @param key
     * @param field
     * @return
     */
    public  Boolean hexists(String key, String field) {
        Jedis jedis = null;
        Boolean exists = null;
        try {
            jedis = getJedis();
            exists = jedis.hexists(key, field);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return exists;
    }

    /**
     * 返回某个hash表中所有的key
     *
     * @param key
     * @return
     */
    public  Set<String> hkeys(String key) {
        Jedis jedis = null;
        Set<String> keys = null;
        try {
            jedis = getJedis();
            keys = jedis.hkeys(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return keys;
    }

    /**
     * 返回某个hash表中所有的value
     *
     * @param key
     * @return
     */
    public  List<String> hvals(String key) {
        Jedis jedis = null;
        List<String> hvals = null;
        try {
            jedis = getJedis();
            hvals = jedis.hvals(key);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return hvals;
    }

    /**
     * 删除某个hash表中的一个或多个键值对
     *
     * @param key
     * @param fields
     */
    public  void hdel(String key, String... fields) {
        Jedis jedis = null;
        try {
            jedis = getJedis();
            jedis.hdel(key, fields);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
    }

    /**
     * 给某个hash表中的某个field的value增加多少
     *
     * @param key       hash表的key
     * @param field     表中的某个field
     * @param increment 增加多少
     * @return
     */
    public  Long hincrby(String key, String field, Long increment) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = getJedis();
            result = jedis.hincrBy(key, field, increment);
        }finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return result;
    }

案例

hset 001 001 'tom'

注:Value 可以是JSON字符串

JSON 转实体

获取key对应map中的一个value

hget key map<key, value>

案例

hget 001 002

在这里插入图片描述

获取key对应的map所有value

如下,想要获取key为d74a9692c9的map

在这里插入图片描述

hgetall key

案例

hgetall d74a9692c9

在这里插入图片描述

删除hash

del key

案例

del FLINK:PREPROCESSING:HisCurveRegion20220710

更多Redis操作命令可以参考:

Redis

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BirdMan98

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值