Amir Rossert
Amir Rossert

Reputation: 1053

Redis iterate over key groups

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?

Upvotes: 3

Views: 10667

Answers (2)

Nikita Koksharov
Nikita Koksharov

Reputation: 10793

You can use Redis based java.util.Iterator and java.lang.Iterable interfaces offered by Redisson Redis client.

Here is an example:

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)) {
...
}

Upvotes: 0

skeller88
skeller88

Reputation: 4504

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

Upvotes: 14

Related Questions