redis基本操作——hash

database

一、将哈希表 key 中的字段 field 的值设为 value

  命令:hset

  格式:hset key field value

127.0.0.1:6379> hset hash_key key1 v1

(integer) 1

127.0.0.1:6379> hset hash_key key2 v2

(integer) 1

二、获取存储在哈希表中指定字段的值

  命令:hget

  格式:hget key field

127.0.0.1:6379> hget hash_key key1

"v1"

127.0.0.1:6379> hget hash_key key2

"v2"

三、删除一个或多个哈希表字段

  命令:hdel

  格式:hdel key field

127.0.0.1:6379> hget hash_key key1

"v1"

127.0.0.1:6379> hget hash_key key2

"v2"

127.0.0.1:6379> hdel hash_key key2

(integer) 1

127.0.0.1:6379> hget hash_key key2

(nil)

127.0.0.1:6379> hget hash_key key1

"v1"

四、查看哈希表 key 中,指定的字段是否存在

  命令:hexists

  格式:hexists key field

127.0.0.1:6379> hget hash_key key2

(nil)

127.0.0.1:6379> hget hash_key key1

"v1"

127.0.0.1:6379> hexists hash_key key1

(integer) 1

127.0.0.1:6379> hexists hash_key key2

(integer) 0

五、获取在哈希表中指定 key 的所有字段和值

  命令:hgetall

  格式:hgetall key

127.0.0.1:6379> hset hash_key key1 v1

(integer) 1

127.0.0.1:6379> hgetall hash_key

1) "key1"

2) "v1"

127.0.0.1:6379> hset hash_key key2 v2

(integer) 1

127.0.0.1:6379> hgetall hash_key

1) "key1"

2) "v1"

3) "key2"

4) "v2"

127.0.0.1:6379> hdel hash_key key2

(integer) 1

127.0.0.1:6379> hgetall hash_key

1) "key1"

2) "v1"

六、获取所有哈希表中的字段

  命令:hkeys

  格式:kheys key

127.0.0.1:6379> hset hash_key key1 v1

(integer) 1

127.0.0.1:6379> hkeys hash_key

1) "key1"

127.0.0.1:6379> hset hash_key key2 v2

(integer) 1

127.0.0.1:6379> hkeys hash_key

1) "key1"

2) "key2"

七、获取哈希表中字段的数量

  命令:hlen

  格式:hlen key

127.0.0.1:6379> hset hash_key key1 v1

(integer) 1

127.0.0.1:6379> hlen hash_key

(integer) 1

127.0.0.1:6379> hset hash_key key2 v2

(integer) 1

127.0.0.1:6379> hlen hash_key

(integer) 2

八、同时将多个 field-value (域-值)对设置到哈希表 key 中

  命令:hmset

  格式:hmset key field1 value1 [field2 value2] ...

127.0.0.1:6379> hgetall hash_key

1) "key1"

2) "v1"

3) "key2"

4) "v2"

127.0.0.1:6379> hkeys hash_key

1) "key1"

2) "key2"

127.0.0.1:6379> hmset hash_key key3 v3 key4 v4 key5 v5

OK

127.0.0.1:6379> hgetall hash_key

1) "key1"

2) "v1"

3) "key2"

4) "v2"

5) "key3"

6) "v3"

7) "key4"

8) "v4"

9) "key5"

10) "v5"

127.0.0.1:6379> hkeys hash_key

1) "key1"

2) "key2"

3) "key3"

4) "key4"

5) "key5"

九、获取所有给定字段的值

  命令:hmget

  格式:hmget key field1 [field2] ...

127.0.0.1:6379> hmget hash_key key1 key3 key5

1) "v1"

2) "v3"

3) "v5"

十、获取哈希表中所有值

  命令:hvals

  格式:hvals key

127.0.0.1:6379> hgetall hash_key

1) "key1"

2) "v1"

3) "key2"

4) "v2"

5) "key3"

6) "v3"

7) "key4"

8) "v4"

9) "key5"

10) "v5"

127.0.0.1:6379> hvals hash_key

1) "v1"

2) "v2"

3) "v3"

4) "v4"

5) "v5"

十一、只有在字段 field 不存在时,设置哈希表字段的值

  命令:hsetnx

  格式:hsetnx key field value

127.0.0.1:6379> hset hash_key key1 value1

(integer) 1

127.0.0.1:6379> hget hash_key key1

"value1"

127.0.0.1:6379> hset hash_key key1 value100

(integer) 0

127.0.0.1:6379> hget hash_key key1

"value100"

127.0.0.1:6379> hsetnx hash_key key1 value10000

(integer) 0

127.0.0.1:6379> hget hash_key key1

"value100"

127.0.0.1:6379> hsetnx hash_key key10 value10000

(integer) 1

127.0.0.1:6379> hget hash_key key10

"value10000"

十二、为哈希表 key 中的指定字段的整数值加上增量 increment

  命令:hincrby

  格式:hincrby key field increment

127.0.0.1:6379> hset hash_key num 100

(integer) 1

127.0.0.1:6379> hget hash_key num

"100"

127.0.0.1:6379> hincrby hash_key num 5

(integer) 105

127.0.0.1:6379> hget hash_key num

"105"

127.0.0.1:6379> hincrby hash_key num -15

(integer) 90

127.0.0.1:6379> hget hash_key num

"90"

十三、为哈希表 key 中的指定字段的浮点数值加上增量 increment

  命令:hincrbyfloat

  格式:hincrbyfloat key field increment

127.0.0.1:6379> hset hash_key float100

(integer) 1

127.0.0.1:6379> hget hash_key float

"100"

127.0.0.1:6379> hincrbyfloat hash_key float0.5

"100.5"

127.0.0.1:6379> hget hash_key float

"100.5"

127.0.0.1:6379> hincrbyfloat hash_key float -1.5

"99"

127.0.0.1:6379> hget hash_key float

"99"

十四、迭代哈希表中的键值对

  命令:hscan

  格式:hscan key cursor [MATCH pattern][COUNT count]

 

127.0.0.1:6379> hmset hash_key k1 v1 k2 v2 k3 v3 k4 v4 k5 v5 k6 v6 k7 v7 k8 v8 k9 v9 k10 v10 k11 v11 k12 v12

OK

127.0.0.1:6379> hgetall hash_key

1) "k1"

2) "v1"

3) "k2"

4) "v2"

5) "k3"

6) "v3"

7) "k4"

8) "v4"

9) "k5"

10) "v5"

11) "k6"

12) "v6"

13) "k7"

14) "v7"

15) "k8"

16) "v8"

17) "k9"

18) "v9"

19) "k10"

20) "v10"

21) "k11"

22) "v11"

23) "k12"

24) "v12"

127.0.0.1:6379> del hash_key

(integer) 1

127.0.0.1:6379> hmset hash_key key1 v1 key2 v2 key3 v3 key4 v4 key5 v5 k6 v6 k7 v7 k8 v8 k9 v9 k10 v10

OK

127.0.0.1:6379> hgetall hash_key

1) "key1"

2) "v1"

3) "key2"

4) "v2"

5) "key3"

6) "v3"

7) "key4"

8) "v4"

9) "key5"

10) "v5"

11) "k6"

12) "v6"

13) "k7"

14) "v7"

15) "k8"

16) "v8"

17) "k9"

18) "v9"

19) "k10"

20) "v10"

127.0.0.1:6379> hscan hash_key 0 match k*

1) "0"

2) 1) "key1"

2) "v1"

3) "key2"

4) "v2"

5) "key3"

6) "v3"

7) "key4"

8) "v4"

9) "key5"

10) "v5"

11) "k6"

12) "v6"

13) "k7"

14) "v7"

15) "k8"

16) "v8"

17) "k9"

18) "v9"

19) "k10"

20) "v10"

127.0.0.1:6379> hscan hash_key 0 match key*

1) "0"

2) 1) "key1"

2) "v1"

3) "key2"

4) "v2"

5) "key3"

6) "v3"

7) "key4"

8) "v4"

9) "key5"

10) "v5"

 

以上是 redis基本操作——hash 的全部内容, 来源链接: utcz.com/z/533924.html

回到顶部