Redis操作数据结构-Hash
-
Redis中的数据结构hash,它是String数据结构的升级版。类似于*java中的Map一样
Map<String,Map<String,String>> hash = new HashMap<String,HashMap<String,String>>();
-
每个hash存储的大小,可以存储2的(32-1)次方个键值对(40多亿条)。
hset key field value [field value …] 给hash添加值 – map.put(key,value)
127.0.0.1:6379> hset course:100 title java
(integer) 1
HGET key field 获取hash中key对应的值 – map.get(key)
127.0.0.1:6379> hget course:100 title
"java"
HMSET key field1 value1 field2 value2 … 批量给hash添加值
127.0.0.1:6379> hmset course:100 title java price 12 detail veryverygood
OK
HMGET key field1 field2 … 批量获取hash中key对应的值
127.0.0.1:6379> hmget course:100 title price detail
1) "java"
2) "12"
3) "veryverygood
HKEYS key 获取hash所有的key
127.0.0.1:6379> hkeys course:100
1) "title"
2) "price"
3) "detail"
HVALS key 获取hash所有的value
127.0.0.1:6379> hvals course:100
1) "java"
2) "12"
3) "veryverygood"
HGETALL key 获取hash中所有的filed和value
127.0.0.1:6379> hgetall course:100
1) "title"
2) "java"
3) "price"
4) "12"
5) "detail"
6) "veryverygood"
127.0.0.1:6379>
HLEN key 获取hash中元素的个数
127.0.0.1:6379> hlen course:100
(integer) 3
HINCRBY key field value 给hash中key对应的field的value递增
127.0.0.1:6379> hincrby course:100 price 1
(integer) 13
127.0.0.1:6379> hincrby course:100 price 1
(integer) 14
127.0.0.1:6379> hincrby course:100 price 1
(integer) 15
HINCRBYFLOAT key field value 给hash中key对应的field的value浮点递增
127.0.0.1:6379> hincrbyfloat course:100 price 1.2
"16.2"
127.0.0.1:6379> hincrbyfloat course:100 price 1.2
"17.4"
127.0.0.1:6379> hincrbyfloat course:100 price 1.2
"18.6"
HEXISTS key filed 判断hash中key对应的field存在与否
127.0.0.1:6379> HEXISTS course:100 price
(integer) 1
127.0.0.1:6379> HEXISTS course:100 address
(integer) 0
HDEL key field [field1,field2…] 删除hash中key对应的field元素
127.0.0.1:6379> hdel course:100 address title
(integer) 1