Is there a possibility of fetching n number of keys at a time in Redis?
I have more than a million keys in a Redis and I want to prepare a csv file of 100k records each. I want to fetch 100k keys and prepare the files.
Is there a possibility of fetching n number of keys at a time in Redis?
I have more than a million keys in a Redis and I want to prepare a csv file of 100k records each. I want to fetch 100k keys and prepare the files.
2
Answers
Use
RedisTemplate
, By using akeys
method you can get all keys in a Redis like below:Spring doc : RedisTemplate
Using Java8 :
You can use
SCAN
command withCOUNT
option. (link)The following code sample uses jedis as the redis client.
Here I am fetching 2 keys in every iteration of the scan command. The cursor value returned in the
ScanResult
is sent as input to the next scan command. If there are no more results, cursor value is “0”. This is used for signalling the termination of the for loop.I saw the following output on running this sample.