一 点睛
Hash类型用于存储结构化数据。
Hash可以看作是Map中的Map。
二 实战
1 HSET命令:为哈希的域设值
127.0.0.1:6379> HSET student1 stuName 'liuhefei'
1
127.0.0.1:6379> HSET student1 stuID 20190102
1
127.0.0.1:6379> HSET student1 age 22
1
127.0.0.1:6379> HSET student1 sex 'nan'
1
127.0.0.1:6379> HSET student1 height 171
1
127.0.0.1:6379> HSET student1 weight 75
1
127.0.0.1:6379> HSET student1 birthday 1996-02-14
1
127.0.0.1:6379> HSET student1 className 'ruanjian1ban'
1
2 HSETNX命令:为哈希表不存在的域设值
127.0.0.1:6379> HSETNX student1 motto 'pinggenisiwohuo'
1
127.0.0.1:6379> HSETNX student1 motto 'pinggechuxiaqiudong'
0
3 HMSET命令:设置多个域和值到哈希表中
127.0.0.1:6379> HMSET student2 stuName 'xiaowang' stuID 20180920 age 24 sex 'nan' height 174 weight 71
OK
4 HGET命令:获取哈希表中域的值
127.0.0.1:6379> HGET student1 stuName
liuhefei
127.0.0.1:6379> HGET student1 stuID
20190102
127.0.0.1:6379> HGET student1 age
22
127.0.0.1:6379> HGET student1 sex
nan
127.0.0.1:6379> HGET student1 height
171
127.0.0.1:6379> HGET student1 weight
75
127.0.0.1:6379> HGET student1 birthday
1996-02-14
127.0.0.1:6379> HGET student1 className
ruanjian1ban
5 HGETALL命令:获取哈希表中所有的域和值
127.0.0.1:6379> HGETALL student2
stuName
xiaowang
stuID
20180920
age
24
sex
nan
height
174
weight
71
6 HMGET命令:获取多个域的值
127.0.0.1:6379> HMGET student1 stuName stuID age birthday className
liuhefei
20190102
22
1996-02-14
ruanjian1ban
7 HKEYS命令:获取哈希表中所有域
127.0.0.1:6379> HKEYS student1
stuName
stuID
age
sex
height
weight
birthday
className
motto
8 HVALS命令:获取哈希表中所有域的值
127.0.0.1:6379> HVALS student1
liuhefei
20190102
22
nan
171
75
1996-02-14
ruanjian1ban
pinggenisiwohuo
9 HLEN命令:统计哈希表中域的数量
127.0.0.1:6379> HKEYS student2
stuName
stuID
age
sex
height
weight
127.0.0.1:6379> HLEN student2
6
10 HSTRLEN:统计域的值的字符串长度
127.0.0.1:6379> HSTRLEN student1 stuName
8
127.0.0.1:6379> HSTRLEN student1 stuID
8
127.0.0.1:6379> HSTRLEN student1 className
12
127.0.0.1:6379> HSTRLEN student1 motto
15
127.0.0.1:6379> HSTRLEN student className
0
11 HINCRBY命令:为哈希表中的域加上增量值
127.0.0.1:6379> HINCRBY student1 age 3
25
127.0.0.1:6379> HINCRBY student1 height 3
174
127.0.0.1:6379> HINCRBY student1 weight 3
78
127.0.0.1:6379> HINCRBY student1 className 3
ERR hash value is not an integer
12 HINCRBYFLOAT命令:为哈希表中的域加上浮点数增量值
127.0.0.1:6379> HINCRBYFLOAT student2 height 3.56
177.56
127.0.0.1:6379> HINCRBYFLOAT student2 weight 4.43
75.430000000000007
127.0.0.1:6379> HINCRBYFLOAT student2 className 2.54
2.54
13 HDEL命令:删除哈希表中的多个域
127.0.0.1:6379> HDEL student2 height weight
2
127.0.0.1:6379> HDEL student2 height weight
0
14 HEXISTS命令:判断哈希表中的域是否存在
127.0.0.1:6379> HEXISTS student1 stuName
1
127.0.0.1:6379> HEXISTS student1 age
1
127.0.0.1:6379> HEXISTS student1 height
1
127.0.0.1:6379> HEXISTS student3 className
0