Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis iterate over key groups

Tags:

redis

jedis

I'm would like to check that all my keys in redis are correct.

I'm storing the keys in groups like so:

userid:fname
userid:lname
userid:age
...

I would like to iterate over the them by grouping them by userid and then check each group from fname, lname and age.

How can I do this?

like image 622
Amir Rossert Avatar asked Apr 21 '26 10:04

Amir Rossert


1 Answers

ScanParams params = new ScanParams();
params.match("userid:fname*");

// Use "0" to do a full iteration of the collection.  
ScanResult<String> scanResult = jedis.scan("0", params);
List<String> keys = scanResult.getResult();

Repeat above code for lname and age. Or, match user:id and then filter the groups using a regex and iterating through keys.

EDIT: For large collections (millions of keys), a scan result will return a few tens of elements. Adjust your code accordingly to continue scanning until the whole collection of keys has been scanned:

ScanParams params = new ScanParams();
params.match("userid:fname*");

// An iteration starts at "0": http://redis.io/commands/scan
ScanResult<String> scanResult = jedis.scan("0", params);
List<String> keys = scanResult.getResult();
String nextCursor = scanResult.getStringCursor();
int counter = 0;

while (true) {
    for (String key : keys) {
        addKeyToProperGroup(key);
    }

    // An iteration also ends at "0"
    if (nextCursor.equals("0")) {
        break;
    }

    scanResult = jedis.scan(nextCursor, params);
    nextCursor = scanResult.getStringCursor();
    keys = scanResult.getResult();
}
like image 199
skeller88 Avatar answered Apr 23 '26 13:04

skeller88



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!