Redis字典 Hash

Redis字典 Hash

  • 一个key对应一个hash
  • 一个hash中又是一个key对应一个value

使用redis-cli

  • 插入和查询;
47:0>hset user name lc age 18
2
47:0>hget user name
lc
  • 插入和查询多个;
47:0>hmset user1 name lc666 age 18
OK
47:0>hmget user1 name age
1) lc666
2) 18
  • 查询某个hash的所有key
47:0>hkeys user1
1) name
2) age
  • 查询某个hash所有value
47:0>hvals user1
1) lc666
2) 18
  • 获取某个hash所有内容;
47:0>hgetall user1
1) name
2) lc666
3) age
4) 18
  • 查询某个hash中是否存在某个key
47:0>hexists user1 name
1
  • 查询某个hash中元素个数:
47:0>hlen user1
2
  • 修改已存在的hash,如果修改已经存在的值,值不会改变;
47:0>hsetnx user gender m
1
47:0>hsetnx user name lc666
0
47:0>hget user name
lc
  • 删除key中的一个或者多个元素;
47:0>hdel user name age
2
47:0>hget user name
NULL

Java代码操作

public class RedisHash {
    public static void main(String[] args) throws InterruptedException {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        String redisKey = "user";   
        jedis.hset(redisKey, "key1", "value1");
        
        Map<String, String> singleMap = jedis.hgetAll(redisKey);
        System.out.println(singleMap.get("key1"));  
        Map<String, String> allMap = jedis.hgetAll(redisKey);
        System.out.println(allMap.get("k2"));
        System.out.println(allMap);                 
        Long delResult = jedis.hdel(redisKey, "key1");
        System.out.println("删除结果:" + delResult);    
        System.out.println(jedis.hget(redisKey, "key1")); 
    }
}

数据结构

  • 由数组加链表构成,类似于Java中的HashMap
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • NOSQL类型简介键值对:会使用到一个哈希表,表中有一个特定的键和一个指针指向特定的数据,如redis,volde...
    MicoCube阅读 4,078评论 2 27
  • 1 Redis介绍1.1 什么是NoSql为了解决高并发、高可扩展、高可用、大数据存储问题而产生的数据库解决方...
    克鲁德李阅读 5,371评论 0 36
  • Ubuntu下安装redis 安装redis 在 Ubuntu 系统安装 Redi 可以使用以下命令: 启动 Re...
    riverstation阅读 975评论 0 0
  • Redis是啥 Redis是一个开源的key-value存储系统,由于拥有丰富的数据结构,又被其作者戏称为数据结构...
    一凡呀阅读 1,184评论 0 5
  • redis是一个以key-value存储的非关系型数据库。有五种数据类型,string、hashes、list、s...
    林ze宏阅读 1,023评论 0 0