Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCAN command with spring redis template

I am trying to execute "scan" command with RedisConnection. I don't understand why the following code is throwing NoSuchElementException

RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
    Cursor c = redisConnection.scan(scanOptions);
    while (c.hasNext()) {
        c.next();
    }

Exception:

java.util.NoSuchElementException at java.util.Collections$EmptyIterator.next(Collections.java:4189) at org.springframework.data.redis.core.ScanCursor.moveNext(ScanCursor.java:215) at org.springframework.data.redis.core.ScanCursor.next(ScanCursor.java:202)

like image 918
coolscitist Avatar asked Sep 23 '15 04:09

coolscitist


People also ask

What is Scan Command in Redis?

As mentioned, the SCAN in Redis is a cursor-based iterator that allows you to iterate over the set of keys in a specific Redis database. The command accepts the cursor position as the argument. The server returns an update cursor every time the command is called.

What is spring data Redis example?

For example, instead of dealing with high-level objects, we are sending a stream of bytes, using ByteBuffer. Also, we use more of the Redis primitives like SET and SCAN. Finally, String and Key Commands are just two among many command interfaces that Spring Data Redis exposes reactively.

What is the difference between hscan and sscan in Redis?

SCAN iterates the set of keys in the currently selected Redis database. SSCAN iterates elements of Sets types. HSCAN iterates fields of Hash types and their associated values. ZSCAN iterates elements of Sorted Set types and their associated scores.

What is a Redis template?

The template is, in fact, the central class of the Redis module, due to its rich feature set. The template offers a high-level abstraction for Redis interactions.


1 Answers

Yes, I have tried this, in 1.6.6.RELEASE spring-data-redis.version. No issues, the below simple while loop code is enough. And i have set count value to 100 (more the value) to save round trip time.

    RedisConnection redisConnection = null;
    try {
        redisConnection = redisTemplate.getConnectionFactory().getConnection();
        ScanOptions options = ScanOptions.scanOptions().match(workQKey).count(100).build();

        Cursor c = redisConnection.scan(options);
        while (c.hasNext()) {
            logger.info(new String((byte[]) c.next()));
        }
    } finally {
        redisConnection.close(); //Ensure closing this connection.
    }
like image 104
Kanagavelu Sugumar Avatar answered Sep 29 '22 12:09

Kanagavelu Sugumar