当前位置:   article > 正文

redis使用scan遍历数据_redis scan命令遍历数据

redis scan命令遍历数据

有时候我们想获取所有匹配的key,或者遍历zset、set、hash中的数据,之前我们更多的是通过keys命令或range获取数据,但是我们知道redis处理命令是单线程的,这样就有可能因为我们的命令执行耗时比较久引起服务端问题。在redis中提供了scan命令,这个命令类似数据库中的游标,每次只返回一部分数据,这样就不会因为数据量大导致命令阻塞的问题,对比scan命令,redis还提供了hscan、zscan、sscan等命令。下面介绍一下通过jedis客户端使用scan命令,示例代码中是通过使用zscan命令,其他的命令大致一样:

import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.Tuple;

import java.util.List;

public void testZscan() {
    ScanParams params = new ScanParams();
    params.count(500);
    String cursor = "0";
    ScanResult<Tuple> zscanResult = jedis.zscan("test:zsets", cursor, params);
    boolean finished = false;
    int count = 0;
    while (!finished) {
        List<Tuple> result = zscanResult.getResult();
        if(result != null && result.size() != 0) {
            count += result.size();
            for(Tuple tuple : result) {
                System.out.println(tuple.getScore() + "|" + tuple.getElement());
            }

            cursor = zscanResult.getCursor();
            if("0".equals(cursor)) {
                finished = true;
            }
            zscanResult = jedis.zscan("test:zsets", cursor, params);
        } else {
            finished = true;
        }
    }
    System.out.println("遍历数据条数:" + count);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

现在更多是通过redisTemplate调用redis,redisTemplate对常用的redis命令做了封装,使用redis命令方式如下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.nio.charset.StandardCharsets;

public void testZscan() {
    Integer rs = redisTemplate.execute((RedisCallback<Integer>) connection -> {
        ScanOptions options = ScanOptions.scanOptions().count(500).build();

        Cursor<RedisZSetCommands.Tuple> cursor = connection.zScan("test:zsets".getBytes(StandardCharsets.UTF_8), options);
        int count = 0;
        if (cursor != null) {
            while (cursor.hasNext()) {
                RedisZSetCommands.Tuple tuple = cursor.next();
                System.out.println(tuple.getScore() + "|" + new String(tuple.getValue(), StandardCharsets.UTF_8));
                count++;
            }
        }

        return count;
    });
    System.out.println("遍历数据条数:" + rs);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/658608
推荐阅读
相关标签
  

闽ICP备14008679号