Extracting Keys From Redis

java, jedis, redis

Solution

When Redis server store many record, using `jedis.keys()` command can make it over-processing. Thus, result in it stop when the task is un-done. Instead of it, you use `jedis.hscan()` so as to avoid above problem.

ScanParams scanParam = new ScanParams();
String cursor = "0";
int i = 0;
scanParam.match(prefix + "*");
scanParam.count(50000); // 2000 or 30000 depend on your
do {
ScanResult<Map.Entry<String, String>> hscan = jedis.hscan(key, cursor, scanParam);

// any code here
//
//

// check cursor 
cursor = hscan.getStringCursor();
} while("0".equals(cursor));

It work well in your case.

Problem

I use this following code to extract all of the Keys start with "NAME:" and it return only over 5,000 records (There is over 60,000 keys in my index). Can anyone explain why it is happening or how can I extract all of the keys from Redis Database. ``` jedis.select(3); Set<String> names=jedis.keys("NAME:*"); Iterator<String> it = names.iterator(); while (it.hasNext()) { String s = it.next(); System.out.println(s); } ```

Original source