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);
}
Redis GET all Keys To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.
During fetching the records from database,first check whether the key present(primary key i.e id) in redis,if "yes" get data directly from redis,otherwise hit the database for data. where $table is 'posts' or 'comments' etc, and $ids are searched keys. or using lua scripts.
You can simply connect to your redis server using redis-cli, select your database and type KEYS *, please remember it will give you all the keys present in selected redis database.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With