Retrieving keys from Redis

I use this following code to retrieve all the keys with "NAME:" and return only over 5,000 records (my index contains more than 60,000 keys). Can someone explain why this is happening, or how I can extract all the keys from a 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); } 
+4
source share
3 answers

When a Redis server stores many records, the jedis.keys() command can recycle. Thus, when a task stops, it stops. Instead, you use jedis.hscan() to avoid the problem above.

 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)); 

This works well in your case.

+2
source

You should not use the keys method to use Redis normally, as indicated in the JavaDoc below.

http://javadox.com/redis.clients/jedis/2.2.0/redis/clients/jedis/Jedis.html#keys(java.lang.String)

Instead, consider using Redis kits as follows.

 final Jedis jedis = new Jedis("localhost"); jedis.sadd("setName", "someValue"); jedis.smembers("setName"); jedis.close(); 
0
source

Try without a NAME in the key search template.

  Set<String> names = jedis.keys("*"); java.util.Iterator<String> it = names.iterator(); while(it.hasNext()) { String s = it.next(); System.out.println(s + " : " + jedis.get(s)); } 
-2
source

Source: https://habr.com/ru/post/1442937/


All Articles