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