Java Code Examples for redis.clients.jedis.ScanParams#match()

The following examples show how to use redis.clients.jedis.ScanParams#match() . 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: RedisRecordHandler.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
/**
 * For the given key prefix, find all actual keys depending on the type of the key.
 *
 * @param split The split for this request, mostly used to get the redis endpoint and config details.
 * @param redisCursor The previous Redis cursor (aka continuation token).
 * @param keys The collections of keys we collected so far. Any new keys we find are added to this.
 * @return The Redis cursor to use when continuing the scan.
 */
private ScanResult<String> loadKeys(Split split, ScanResult<String> redisCursor, Set<String> keys)
{
    try (Jedis client = getOrCreateClient(split.getProperty(REDIS_ENDPOINT_PROP))) {
        KeyType keyType = KeyType.fromId(split.getProperty(KEY_TYPE));
        String keyPrefix = split.getProperty(KEY_PREFIX_TABLE_PROP);
        if (keyType == KeyType.ZSET) {
            long start = Long.valueOf(split.getProperty(SPLIT_START_INDEX));
            long end = Long.valueOf(split.getProperty(SPLIT_END_INDEX));
            keys.addAll(client.zrange(keyPrefix, start, end));
            return new ScanResult<String>(END_CURSOR, Collections.EMPTY_LIST);
        }
        else {
            String cursor = (redisCursor == null) ? SCAN_POINTER_START : redisCursor.getCursor();
            ScanParams scanParam = new ScanParams();
            scanParam.count(SCAN_COUNT_SIZE);
            scanParam.match(split.getProperty(KEY_PREFIX_TABLE_PROP));

            ScanResult<String> newCursor = client.scan(cursor, scanParam);
            keys.addAll(newCursor.getResult());
            return newCursor;
        }
    }
}
 
Example 2
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 3
Source File: RedisTemplate.java    From jigsaw-payment with Apache License 2.0 6 votes vote down vote up
/**
 * 全局扫描hset
 *
 * @param match field匹配模式
 */
public List<Map.Entry<String, String>> scanHSet(String domain, String match) {
    try (ShardedJedis shardedJedis = shardedJedisPool.getResource()) {
        int cursor = 0;

        ScanParams scanParams = new ScanParams();
        scanParams.match(match);
        Jedis jedis = shardedJedis.getShard(domain);
        ScanResult<Map.Entry<String, String>> scanResult;
        List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>();
        do {
            scanResult = jedis.hscan(domain, String.valueOf(cursor), scanParams);
            list.addAll(scanResult.getResult());
            cursor = Integer.parseInt(scanResult.getStringCursor());
        } while (cursor > 0);
        return list;
    }
}
 
Example 4
Source File: RedisIO.java    From beam with Apache License 2.0 6 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) {
  ScanParams scanParams = new ScanParams();
  scanParams.match(c.element());

  String cursor = ScanParams.SCAN_POINTER_START;
  boolean finished = false;
  while (!finished) {
    ScanResult<String> scanResult = jedis.scan(cursor, scanParams);
    List<String> keys = scanResult.getResult();
    for (String k : keys) {
      c.output(k);
    }
    cursor = scanResult.getCursor();
    if (cursor.equals(ScanParams.SCAN_POINTER_START)) {
      finished = true;
    }
  }
}
 
Example 5
Source File: DefaultRedis.java    From craft-atom with MIT License 5 votes vote down vote up
private ScanResult<Map.Entry<String, Double>> zscan_match_count(Jedis j, String key, String cursor, String pattern, int count) {
	ScanParams param = new ScanParams();
	param.match(pattern);
	param.count(count);
	redis.clients.jedis.ScanResult<Tuple> sr = j.zscan(key, cursor, param);
	return new ScanResult<Map.Entry<String, Double>>(sr.getStringCursor(), convert(sr.getResult()));
}
 
Example 6
Source File: RedisMetadataHandler.java    From aws-athena-query-federation with Apache License 2.0 5 votes vote down vote up
/**
 * For the given zset prefix, find all values and treat each of those values are a key to scan before returning
 * the scan continuation token.
 *
 * @param connStr The Jedis connection string for the table.
 * @param prefix The zset key prefix to scan.
 * @param redisCursor The previous Redis cursor (aka continuation token).
 * @param keys The collections of keys we collected so far. Any new keys we find are added to this.
 * @return The Redis cursor to use when continuing the scan.
 */
private ScanResult<String> loadKeys(String connStr, String prefix, ScanResult<String> redisCursor, Set<String> keys)
{
    try (Jedis client = getOrCreateClient(connStr)) {
        String cursor = (redisCursor == null) ? SCAN_POINTER_START : redisCursor.getCursor();
        ScanParams scanParam = new ScanParams();
        scanParam.count(SCAN_COUNT_SIZE);
        scanParam.match(prefix);

        ScanResult<String> newCursor = client.scan(cursor, scanParam);
        keys.addAll(newCursor.getResult());
        return newCursor;
    }
}
 
Example 7
Source File: DefaultRedis.java    From craft-atom with MIT License 5 votes vote down vote up
private ScanResult<String> sscan_match_count(Jedis j, String key, String cursor, String pattern, int count) {
	ScanParams param = new ScanParams();
	param.match(pattern);
	param.count(count);
	redis.clients.jedis.ScanResult<String> sr = j.sscan(key, cursor, param);
	return new ScanResult<String>(sr.getStringCursor(), sr.getResult());
}
 
Example 8
Source File: DefaultRedis.java    From craft-atom with MIT License 5 votes vote down vote up
private ScanResult<Map.Entry<String, String>> hscan_match_count(Jedis j, String key, String cursor, String pattern, int count) {
	ScanParams param = new ScanParams();
	param.match(pattern);
	param.count(count);
	redis.clients.jedis.ScanResult<Map.Entry<String, String>> sr = j.hscan(key, cursor, param);
	return new ScanResult<Map.Entry<String, String>>(sr.getStringCursor(), sr.getResult());
}
 
Example 9
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 10
Source File: DynoJedisDemo.java    From dyno with Apache License 2.0 5 votes vote down vote up
public void runSScanTest(boolean populateKeys) throws Exception {
    logger.info("SET SCAN TEST -- begin");

    final String key = "DynoClientTest_Set";

    if (populateKeys) {
        logger.info("Populating set in cluster {} with key {}", this.clusterName, key);
        for (int i = 0; i < 50; i++) {
            client.sadd(key, "value-" + i);
        }
    }

    logger.info("Reading members of set from cluster {} with key {}", this.clusterName, key);
    ScanResult<String> scanResult;
    final Set<String> matches = new HashSet<>();
    String cursor = "0";
    do {

        final ScanParams scanParams = new ScanParams().count(10);
        scanParams.match("*");
        scanResult = client.sscan(key, cursor, scanParams);
        matches.addAll(scanResult.getResult());
        cursor = scanResult.getCursor();
        if ("0".equals(cursor)) {
            break;
        }
    } while (true);
    logger.info("SET SCAN TEST -- done");
}
 
Example 11
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 12
Source File: AutoCommandParam.java    From redis-manager with Apache License 2.0 5 votes vote down vote up
public ScanParams buildScanParams() {
    ScanParams scanParams = new ScanParams();
    if (Strings.isNullOrEmpty(this.getCursor())) {
        this.setCursor("0");
    }
    scanParams.count(this.getCount());
    if (Strings.isNullOrEmpty(this.getKey())) {
        this.setKey("*");
    } else if (key.indexOf("*") != 0
            && key.indexOf("*") != (key.length() - 1)) {
        key = "*" + key + "*";
    }
    scanParams.match(this.getKey());
    return scanParams;
}
 
Example 13
Source File: RedisRecordCursor.java    From presto with Apache License 2.0 5 votes vote down vote up
private ScanParams setScanParms()
{
    if (split.getKeyDataType() == RedisDataType.STRING) {
        ScanParams scanParms = new ScanParams();
        scanParms.count(redisJedisManager.getRedisConnectorConfig().getRedisScanCount());

        // when Redis key string follows "schema:table:*" format
        // scan command can efficiently query tables
        // by returning matching keys
        // the alternative is to set key-prefix-schema-table to false
        // and treat entire redis as single schema , single table
        // redis Hash/Set types are to be supported - they can also be
        // used to filter out table data

        // "default" schema is not prefixed to the key

        if (redisJedisManager.getRedisConnectorConfig().isKeyPrefixSchemaTable()) {
            String keyMatch = "";
            if (!split.getSchemaName().equals("default")) {
                keyMatch = split.getSchemaName() + Character.toString(redisJedisManager.getRedisConnectorConfig().getRedisKeyDelimiter());
            }
            keyMatch = keyMatch + split.getTableName() + Character.toString(redisJedisManager.getRedisConnectorConfig().getRedisKeyDelimiter()) + "*";
            scanParms.match(keyMatch);
        }
        return scanParms;
    }

    return null;
}
 
Example 14
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 15
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private ScanResult<Map.Entry<String, String>> hscan_match(Jedis j, String key, String cursor, String pattern) {
	ScanParams param = new ScanParams();
	param.match(pattern);
	redis.clients.jedis.ScanResult<Map.Entry<String, String>> sr = j.hscan(key, cursor, param);
	return new ScanResult<Map.Entry<String, String>>(sr.getStringCursor(), sr.getResult());
}
 
Example 16
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private ScanResult<String> sscan_match(Jedis j, String key, String cursor, String pattern) {
	ScanParams param = new ScanParams();
	param.match(pattern);
	redis.clients.jedis.ScanResult<String> sr = j.sscan(key, cursor, param);
	return new ScanResult<String>(sr.getStringCursor(), sr.getResult());
}
 
Example 17
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private ScanResult<Map.Entry<String, Double>> zscan_match(Jedis j, String key, String cursor, String pattern) {
	ScanParams param = new ScanParams();
	param.match(pattern);
	redis.clients.jedis.ScanResult<Tuple> sr = j.zscan(key, cursor, param);
	return new ScanResult<Map.Entry<String, Double>>(sr.getStringCursor(), convert(sr.getResult()));
}