1、将hash表中key中的域filed的值设置为value:HSET value key filed value ;如果key不存在那么创建一个新的hash表,并且进行HSET,如果filed已经存在就进行覆盖
2、获取hash表中key中的域filed的值:
127.0.0.1:6379> HSET website google "www.google.com"
(integer) 1
127.0.0.1:6379> HGET website google
"www.google.com"
127.0.0.1:6379> HSET website baidu "www.baidu.com"
(integer) 1
127.0.0.1:6379> HLEN website
(integer) 2
3、判断hash表是否存在,判断hash表中key域某个filed是否存在:
127.0.0.1:6379> EXISTS website
(integer) 1
127.0.0.1:6379> EXISTS abdfd
(integer) 0
127.0.0.1:6379> HEXISTS website
(error) ERR wrong number of arguments for 'hexists' command
127.0.0.1:6379> HEXISTS website baidu
(integer) 1
127.0.0.1:6379> keys *
1) "website"
4、如果hash表key域field存在则设置失败,否则设置成功: HSETNX key field value
127.0.0.1:6379> HSET website baidu "www.baibaibaidududu.com"
(integer) 0
127.0.0.1:6379> HGET website baidu
"www.baibaibaidududu.com"
127.0.0.1:6379> HSETNX website sina "www.sina.com"
(integer) 1
127.0.0.1:6379> HGET website sina
"www.sina.com"
127.0.0.1:6379> HSETNX website sina "www.sina.com"
(integer) 0
127.0.0.1:6379> HSETNX website sina "www.sinasinasian.com"
(integer) 0
127.0.0.1:6379> HGET website sina
"www.sina.com"
5、返回hash表中的key下的所有filed和value:
127.0.0.1:6379> keys *
1) "website"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> type website
hash
127.0.0.1:6379> HGETALL website
1) "google"
2) "www.google.com"
3) "baidu"
4) "www.baibaibaidududu.com"
5) "sina"
6) "www.sina.com"
6、在hash表中批量设置批量获取:HMSET key filed value [file value ...] ; HMGET key filed [filed ...]
127.0.0.1:6379> HMSET language china chinese america english jp sbsbsb
OK
127.0.0.1:6379> HMGET language
(error) ERR wrong number of arguments for 'hmget' command
127.0.0.1:6379> HMGET language china america jp
1) "chinese"
2) "english"
3) "sbsbsb"
127.0.0.1:6379> HGETALL language
1) "china"
2) "chinese"
3) "america"
4) "english"
5) "jp"
6) "sbsbsb"
7、返回hash表中key中域的数量: HLEN key
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> HMSET website baidu "www.baidu.com" google "www.google.com" sina "www.sina.com"
OK
127.0.0.1:6379> HLEN website
(integer) 3
127.0.0.1:6379> HSET qq "www.qq.com"
(error) ERR wrong number of arguments for 'hset' command
127.0.0.1:6379> HSET website qq "www.qq.com"
(integer) 1
127.0.0.1:6379> HLEN website
(integer) 4
127.0.0.1:6379> HGETALL website
1) "baidu"
2) "www.baidu.com"
3) "google"
4) "www.google.com"
5) "sina"
6) "www.sina.com"
7) "qq"
8) "www.qq.com"
8、删除hash表中key中单个域或者多个域: HDEL key filed [filed ...]
(integer) 4
127.0.0.1:6379> HGETALL website
1) "baidu"
2) "www.baidu.com"
3) "google"
4) "www.google.com"
5) "sina"
6) "www.sina.com"
7) "qq"
8) "www.qq.com"
127.0.0.1:6379> HDEL website baidu qq
(integer) 2
127.0.0.1:6379> HGETALL website
1) "google"
2) "www.google.com"
3) "sina"
4) "www.sina.com"
9、给hash表中的key field增加增量: HINCRBY KEY file increment ;增量可以是正数 负数 如果key不存在就创建一个hash表 ,如果field不存在就创建一个filed 值初始化为0,如果是一个不可转换的字符串就错误:
127.0.0.1:6379> HGETALL website
1) "google"
2) "www.google.com"
3) "sina"
4) "www.sina.com"
127.0.0.1:6379> HSET website test 123
(integer) 1
127.0.0.1:6379> HGETALL
(error) ERR wrong number of arguments for 'hgetall' command
127.0.0.1:6379> HGETALL website
1) "google"
2) "www.google.com"
3) "sina"
4) "www.sina.com"
5) "test"
6) "123"
127.0.0.1:6379> HINCRBY website test -200
(integer) -77
127.0.0.1:6379> HGET website test
"-77"
10、给hash表中的key field增加浮点增量: HINCRBYFLOAT KEY file increment ;增量可以是正浮点 负浮点 如果key不存在就创建一个hash表 ,如果field不存在就创建一个filed 值初始化为0,如果是一个不可转换的字符串就错误:
127.0.0.1:6379> HSET website myfloat 0
(integer) 1
127.0.0.1:6379> HGETALL website
1) "google"
2) "www.google.com"
3) "sina"
4) "www.sina.com"
5) "test"
6) "-77"
7) "myfloat"
8) "0"
127.0.0.1:6379> HINCRBYFLOAT website myfloat -200.345
"-200.345"
127.0.0.1:6379> HGET website myfloat
"-200.345"
127.0.0.1:6379> HINCRBYFLOAT website aaaafloat 333.4551
"333.4551"
127.0.0.1:6379> HGET website aaaafloat
"333.4551"
127.0.0.1:6379> HGETALL website
1) "google"
2) "www.google.com"
3) "sina"
4) "www.sina.com"
5) "test"
6) "-77"
7) "myfloat"
8) "-200.345"
9) "aaaafloat"
10) "333.4551"