如何读取 Redis 中的集合?

Howto read sets in Redis?

我有一个简单的 redis set,上面有超过 100 万条笔记。我使用 sadd

添加数据

如何按分区读取这个集合?

我的意思是先读取 100 000 个密钥,然后读取 200 000 个密钥?

我不确定你所说的 "by partition" 是什么意思,但如果你想分块阅读,SSCAN 是你的朋友。

SSCAN key cursor [MATCH pattern] [COUNT count]

你从游标中的值0开始,每次都得到下一个游标id和COUNT个元素。当没有更多内容可读时,您会看到光标为 0。

例如:

# let's add 14 elements to a set
127.0.0.1:6379> SADD myset e1 e2 e3 e4 e5 e6 e7 e8 e9 e10 e11 e12 e13 e14
(integer) 14

# now let's scan it from the "beginning" (notice that it's not ordered)
127.0.0.1:6379> SSCAN myset 0 COUNT 10
1) "3"
2)  1) "e8"
    2) "e10"
    3) "e2"
    4) "e11"
    5) "e7"
    6) "e3"
    7) "e14"
    8) "e4"
    9) "e6"
   10) "e9"

# we got a cursor id of 3, let's give that to the next iteration!
127.0.0.1:6379> SSCAN myset 3 COUNT 10
1) "0"
2) 1) "e13"
   2) "e12"
   3) "e5"
   4) "e1"

# now we got a cursor id of 0, meaning we're done