redis的数据类型-Hashes类型及java实现

Hashes介绍:

  • hset
    (user:001是hash表的表名)
127.0.0.1:6379> hset user:001  name liudy
(integer) 1
127.0.0.1:6379> hget user:001 name
  • hsetnx(键相同,返回0,不更新;与setnx类似)
  • hmset(批量设置)
127.0.0.1:6379> hmset user:003 name liudy age 20 sex  1
OK
  • hget
  • hmget
127.0.0.1:6379> hmget user:003 name age sex
1) "liudy"
2) "20"
3) "1"
  • hincrby
127.0.0.1:6379> hincrby user:003 age 5
(integer) 25
  • hexists
    (hash表里面的字段是否存在)
127.0.0.1:6379> hexists user:003 name
(integer) 1
127.0.0.1:6379> hexists user:003 age
(integer) 1
127.0.0.1:6379> hexists user:003 tal
(integer) 0
  • hlen
    (返回键的个数)
127.0.0.1:6379> hlen user:003
(integer) 3
127.0.0.1:6379> 
  • hdel
    删除指定的field字段
127.0.0.1:6379> hlen user:003
(integer) 3
127.0.0.1:6379> hdel user:003 age
(integer) 1
127.0.0.1:6379> hlen user:003
(integer) 2
127.0.0.1:6379> 
  • hkeys
    返回哈希表中的所有字段
127.0.0.1:6379> hkeys user:003
1) "name"
2) "sex"
  • hvals
    返回哈希表中所有的values
127.0.0.1:6379> hkeys user:003
1) "name"
2) "sex"
127.0.0.1:6379> hvals user:003
1) "liudy"
2) "1"
  • hgetall(常用)
    获取全部的field及value
127.0.0.1:6379> hgetall user:003
1) "name"
2) "liudy"
3) "sex"
4) "1"
public class HashTest {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.auth("redis");
        // hset
        System.out.println("***hset***");
        jedis.hset("user:001", "name", "liudy");
        // hsetnx
        System.out.println("***hsetnx***");
        jedis.hsetnx("user:001","name","liudy11");
        jedis.hsetnx("user:003","name","liudy3");
        System.out.println(jedis.hget("user:001","name"));// liudy
        System.out.println(jedis.hget("user:003","name"));
        // hmset
        System.out.println("***hmset***");
        Map map = new HashMap<String, String>();
        map.put("name", "liudy2");
        map.put("age", "20");
        map.put("sex","male");
        jedis.hmset("user:002", map);
        jedis.hmset("user:004", map);
        // hget
        System.out.println("***hget***");
        System.out.println(jedis.hget("user:001","name"));
        // hmget
        System.out.println("***hmget***");
        System.out.println(jedis.hmget("user:002","name","age","sex"));
        // hincrby
        System.out.println("***hincrby***");
        jedis.hincrBy("user:002","age",5);
        System.out.println(jedis.hget("user:002","age"));
        // hexists
        System.out.println("***hexists***");
        System.out.println(jedis.hexists("user:002","age"));
        // hlen
        System.out.println("***hlen***");
        System.out.println(jedis.hlen("user:002"));
        // hdel
        System.out.println("***hdel***");
        jedis.hdel("user:002", "age", "sex", "name");
        // hkeys
        System.out.println("***hkeys***");
        System.out.println(jedis.hkeys("user:004"));
        // hvals
        System.out.println("***hvals***");
        System.out.println(jedis.hvals("user:004"));
        // hgetall(常用)
        System.out.println("***hgetall***");
        System.out.println(jedis.hgetAll("user:004"));

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值