skip to Main Content

Is there a possibility of fetching n number of keys at a time in Redis?

I have more than a million keys in a Redis and I want to prepare a csv file of 100k records each. I want to fetch 100k keys and prepare the files.

2

Answers


  1. Use RedisTemplate, By using a keys method you can get all keys in a Redis like below:

    Spring doc : RedisTemplate

    Set<String> allAvailableKeys = redisTemplate.keys("*"));
    Iterator<String> iterator = allAvailableKeys.iterator();
    List<String> allAvailableKeyList = new ArrayList<>();
    while (iterator.hasNext()) {
           String key= iterator.next();
           allAvailableKeyList.add(key);
    }
    

    Using Java8 :

    Set<String> allAvailableKeys = redisTemplate.keys("*"));
    List<String> allAvailableKeyList =allAvailableKeys.stream.collect(Collectors.toList());
    
    Login or Signup to reply.
  2. You can use SCAN command with COUNT option. (link)

    The following code sample uses jedis as the redis client.

        // Assuming redis is running on port 32768 on localhost
        // insert some records
        Jedis jedis = new Jedis("localhost", 32768);
        jedis.set("foo1", "bar1");
        jedis.set("foo2", "bar2");
        jedis.set("foo3", "bar3");
        jedis.set("foo4", "bar4");
        jedis.set("foo5", "bar5");
    
        // first value of cursor must be "0"
        String cursor = "0";
    
        // fetch 2 keys in every scan
        ScanParams scanParams = new ScanParams().count(2);
    
        do {
            ScanResult<String> scanResult = jedis.scan(cursor, scanParams);
    
            System.out.println("Keys for cursor ---> " + cursor);
            scanResult.getResult().forEach((key) -> {
                System.out.println("Key = " + key);
            });
    
            cursor = scanResult.getCursor();
        } while (!"0".equals(cursor));
    

    Here I am fetching 2 keys in every iteration of the scan command. The cursor value returned in the ScanResult is sent as input to the next scan command. If there are no more results, cursor value is “0”. This is used for signalling the termination of the for loop.

    I saw the following output on running this sample.

    Keys for cursor ---> 0
    Key == foo1
    Key == foo3
    Keys for cursor ---> 4
    Key == foo2
    Key == foo
    Keys for cursor ---> 1
    Key == foo5
    Key == foo4
    Keys for cursor ---> 5
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search