Java Code Examples for redis.clients.jedis.Jedis#scan()

The following examples show how to use redis.clients.jedis.Jedis#scan() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisService.java    From seckill with Apache License 2.0 6 votes vote down vote up
public List<String> scanKeys(String key) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        List<String> keys = new ArrayList<String>();
        String cursor = "0";
        ScanParams sp = new ScanParams();
        sp.match("*" + key + "*");
        sp.count(100);
        do {
            ScanResult<String> ret = jedis.scan(cursor, sp);
            List<String> result = ret.getResult();
            if (result != null && result.size() > 0) {
                keys.addAll(result);
            }
            //再处理cursor
            cursor = ret.getStringCursor();
        } while (!cursor.equals("0"));
        return keys;
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
 
Example 2
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public ScanResult<String> scan(final long cursor, final ScanParams params) {
    ShardedJedis shardedJedis = null;
    try {
        shardedJedis = POOL.getJedis(config.getRedisType());
        final Collection<Jedis> allJedis = shardedJedis.getAllShards();
        if (allJedis.size() == 1) {
            final Jedis jedis = allJedis.iterator().next();
            if (params == null) {
                return jedis.scan(String.valueOf(cursor), DEFAULT_SCAN_PARAMS);
            } else {
                return jedis.scan(String.valueOf(cursor), params);
            }
        } else {
            throw new RedisClientException("不支持对多节点Sharded模式进行Scan操作");
        }
    } catch (final Throwable e) {
        if (e instanceof RedisClientException) {
            throw e;
        }

        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(shardedJedis);
    }
}
 
Example 3
Source File: ScanCursor.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the actual scan command using the native client implementation.
 * The given {@literal options} are never {@code null}.
 * 
 * @param jedis
 * @return
 */
protected synchronized ScanIterable<byte[]> doScanNode(Jedis jedis) {
	ScanResult<byte[]> res = jedis.scan(getCursor().getCursorByteArray(), params);

	List<byte[]> items = res.getResult();
	if (isEmpty(items)) {
		items = emptyList();
	}

	return new ScanIterable<byte[]>(cursor.setCursor(res.getStringCursor()), items);
}
 
Example 4
Source File: JedisClusterFactory.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
private static void deleteExistingKeys(Jedis node) throws Exception {
  String nextCursor = "0";
  Set<String> matchingKeys = new HashSet<>();
  ScanParams params = new ScanParams();
  params.match("*");

  // get all of the keys for the particular node
  do {
    ScanResult scanResult = node.scan(nextCursor, params);
    List<String> keys = scanResult.getResult();
    nextCursor = scanResult.getCursor();

    matchingKeys.addAll(keys);

  } while (!nextCursor.equals("0"));

  if (matchingKeys.size() == 0) {
    return;
  }

  // we cannot pass all of the keys to del because of the following error:
  // "CROSSSLOT Keys in request don't hash to the same slot"
  // so iterate over and delete them individually.
  for (String key : matchingKeys.toArray(new String[matchingKeys.size()])) {
    node.del(key);
  }
}
 
Example 5
Source File: DefaultRedis.java    From craft-atom with MIT License 5 votes vote down vote up
private ScanResult<String> scan_match_count(Jedis j, String cursor, String pattern, int count) {
	ScanParams param = new ScanParams();
	param.match(pattern);
	param.count(count);
	redis.clients.jedis.ScanResult<String> sr = j.scan(cursor, param);
	return new ScanResult<String>(sr.getStringCursor(), sr.getResult());
}
 
Example 6
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private ScanResult<String> scan0(Jedis j, String cursor) {
	redis.clients.jedis.ScanResult<String> sr = j.scan(cursor);
	return new ScanResult<String>(sr.getStringCursor(), sr.getResult());
}
 
Example 7
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private ScanResult<String> scan_count(Jedis j, String cursor, int count) {
	ScanParams param = new ScanParams();
	param.count(count);
	redis.clients.jedis.ScanResult<String> sr = j.scan(cursor, param);
	return new ScanResult<String>(sr.getStringCursor(), sr.getResult());
}
 
Example 8
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private ScanResult<String> scan_match(Jedis j, String cursor, String pattern) {
	ScanParams param = new ScanParams();
	param.match(pattern);
	redis.clients.jedis.ScanResult<String> sr = j.scan(cursor, param);
	return new ScanResult<String>(sr.getStringCursor(), sr.getResult());
}
 
Example 9
Source File: Scan.java    From tutorials with MIT License 4 votes vote down vote up
public ScanResult<String> scan(Jedis jedis, String cursor, ScanParams scanParams) {
    return jedis.scan(cursor, scanParams);
}