How to use SCAN commands in Jedis

I used redis and jedis for some time and never needed SCAN commands. Now, however, I need to use SCAN commands, in particular hscan. I understand how it works at the redis level, but the jedis Java shell side confuses me. There are ScanResults and ScanParameter , and I have no clear idea of ​​how to use them correctly. There is no documentation for this feature, or at least hard to find. Can someone point out where to find decent examples of how to iterate over a hash using hscan with jedis?

Sorry, I don’t have the code, but what I have tried so far just doesn't make any sense.

+12
iteration redis jedis
source share
4 answers

In a good tradition of answering my own questions, here's what I learned:

 key = "THEKEY"; ScanParams scanParams = new ScanParams().count(100); String cur = redis.clients.jedis.ScanParams.SCAN_POINTER_START; boolean cycleIsFinished = false; while(!cycleIsFinished){ ScanResult<Entry<String, String>> scanResult = jedis.hscan(key, cur, scanParams); List<Entry<String, String>> result = scanResult.getResult(); //do whatever with the key-value pairs in result cur = scanResult.getStringCursor(); if (cur.equals("0")){ cycleIsFinished = true; } } 

The important part is that cur is a String variable and is "0" if validation is complete.

Using ScanParams, I was able to determine the approximate size of each fragment to get a hash. Approximate, because the hash may change during the scan, so it may be that the item is returned twice in a loop.

+15
source share

I don't like flag variables

 Jedis jedis = new Jedis("localhost"); ScanParams scanParams = new ScanParams().count(10).match("*"); String cur = SCAN_POINTER_START; do { ScanResult<String> scanResult = jedis.scan(cur, scanParams); // work with result scanResult.getResult().stream().forEach(System.out::println); cur = scanResult.getStringCursor(); } while (!cur.equals(SCAN_POINTER_START)); 
+9
source share

Proposal for the above example. You can specify a key match in the scanParams class. See below.

 ScanParams scanParams = new ScanParams(); scanParams.match("*"); String cursor = redis.clients.jedis.ScanParams.SCAN_POINTER_START; boolean cycleIsFinished = false; while (!cycleIsFinished) { ScanResult<String> scanResult = jedisRead.scan(cursor, scanParams); List<String> result = scanResult.getResult(); /* * do what you need to do with the result */ cursor = scanResult.getStringCursor(); if (cursor.equals("0")) { cycleIsFinished = true; } } 
+3
source share

If you use java.util.Iterator or java.lang.Iterable interfaces then you can try Redison based on Redison .

Here is an example of how to iterate over all the map keys under the name "myMap" stored in Redis:

 RedissonClient redissonClient = RedissonClient.create(config); // implements java.util.concurrent.ConcurrentMap interface RMap<String, String> map = redissonClient.getMap("myMap"); // default batch size on each HSCAN invocation is 10 for (String key: map.keySet()) { ... } // default batch size on each HSCAN invocation is 250 for (String key: map.keySet(250)) { ... } 

Here is an example of how to iterate over all keys stored in Redis:

 RedissonClient redissonClient = RedissonClient.create(config); RKeys keys = redissonClient.getKeys(); // default batch size on each SCAN invocation is 10 for (String key: keys.getKeys()) { ... } // default batch size on each SCAN invocation is 250 for (String key: keys.getKeys(250)) { ... } 

Very simple, right?

+1
source share

All Articles