Keyscan using reactive

221 views
Skip to first unread message

rjo...@mutualink.net

unread,
Nov 2, 2018, 9:19:28 AM11/2/18
to lettuce-redis-client-users
I've been trying to get a scan working using reactive commands and I can't quite wrap my head around it.  In this example below, I simply want to scan the key and add the results to a list.

I understand how to do it in a non-reactive way using sync():

        ScanArgs scanArgs = ScanArgs.Builder.matches(workName).limit(15);
       
KeyScanCursor<String> keyScanCursor = new KeyScanCursor<>();
        keyScanCursor
.setCursor(ScanCursor.INITIAL.getCursor());
       
List<String> someList = new ArrayList<>();
       
do {
            keyScanCursor
= redisConnection.sync().scan(keyScanCursor, scanArgs);
            someList
.addAll(keyScanCursor.getKeys());
       
} while (!keyScanCursor.isFinished());


But moving to a reactive way of doing it completely throws me off.  If you're trying to call the keyScanCursor again in the mono, you'd just get another mono back. 

Here's an example of the reactive key scan returning a mono.  I've actually tried it a few different ways, so this is just one of the ways (and considering it's not iterating the keyScanCursor, it's not one I believe would work anyway).
        ScanArgs scanArgs = ScanArgs.Builder.matches(workName).limit(15);        
       
Mono<KeyScanCursor<String>> scanKeysMono = redisConnection.reactive().scan(scanArgs);

        scanKeysMono
.map(keyScanCursor -> {
            someArrayList
.addAll(keyScanCursor.getKeys());
            LOG
.debug("Found {} .", someArrayList.size());
           
return 0;
       
}).subscribe();

Mark Paluch

unread,
Nov 4, 2018, 11:58:58 AM11/4/18
to lettuce-redis-client-users, rjo...@mutualink.net
Keyspace Scanning using SCAN commands requires some nontrivial loops to get things right. Lettuce ships with ScanIterator and ScanStream classes. Both wrap SCAN iteration and allow consumption of keys through Iterator respective Flux.

Cheers,
Mark
--
You received this message because you are subscribed to the Google Groups "lettuce-redis-client-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lettuce-redis-clien...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lettuce-redis-client-users/24889efe-43fb-48ea-8d78-3868021d8338%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rjo...@mutualink.net

unread,
Nov 5, 2018, 2:30:34 PM11/5/18
to lettuce-redis-client-users
Thanks Mark.  ScanStream was just the ticket.  I didn't see ScanIterator as part of the reactive api, but maybe it's a higher version that what I recently updated to (5.1).  Anyway, ScanStream was perfect.  
Reply all
Reply to author
Forward
0 new messages