Java Code Examples for redis.clients.jedis.ScanResult#getCursor()

The following examples show how to use redis.clients.jedis.ScanResult#getCursor() . 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: RedisRecordHandler.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
private void loadZSetRows(Jedis client, String keyString, BlockSpiller spiller, List<Field> fieldList)
{
    if (fieldList.size() != 1) {
        throw new RuntimeException("Ambiguous field mapping, more than 1 field for ZSET value type.");
    }

    Field zfield = fieldList.get(0);
    String cursor = SCAN_POINTER_START;
    do {
        ScanResult<Tuple> result = client.zscan(keyString, cursor);
        cursor = result.getCursor();
        for (Tuple nextElement : result.getResult()) {
            spiller.writeRows((Block block, int rowNum) -> {
                Object zvalue = ValueConverter.convert(zfield, nextElement.getElement());
                boolean zsetMatched = block.offerValue(KEY_COLUMN_NAME, rowNum, keyString);
                zsetMatched &= block.offerValue(zfield.getName(), rowNum, zvalue);
                return zsetMatched ? 1 : 0;
            });
        }
    }
    while (cursor != null && !END_CURSOR.equals(cursor));
}
 
Example 3
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 4
Source File: JedisClientDelegate.java    From kork with Apache License 2.0 6 votes vote down vote up
@Override
public void withKeyScan(String pattern, int count, Consumer<RedisScanResult> f) {
  ScanParams params = new ScanParams().match(pattern).count(count);
  String cursor = ScanParams.SCAN_POINTER_START;

  try (Jedis jedis = jedisPool.getResource()) {
    do {
      ScanResult<String> result = jedis.scan(cursor, params);

      final List<String> results = result.getResult();
      f.accept(() -> results);

      cursor = result.getCursor();
    } while (!"0".equals(cursor));
  }
}
 
Example 5
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 6
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 7
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 8
Source File: CursorBasedResultImpl.java    From dyno with Apache License 2.0 5 votes vote down vote up
@Override
public String getCursorForHost(String host) {
    ScanResult<T> sr = result.get(host);
    if (sr != null) {
        return sr.getCursor();
    }

    return "0";
}
 
Example 9
Source File: ExpireHashTest.java    From dyno with Apache License 2.0 5 votes vote down vote up
@Test
public void testScan() {
    String expireHashKey = "expireHashKey";
    final String secondaryKeyPrefix = "secondaryKey-";
    final String valuePrefix = "value-";
    final int fieldCount = 1000;
    final long minTimeout = 15; //seconds

    Map<String, Pair<String, Long>> fields = new HashMap<>();
    for (int i = 1; i <= fieldCount; i++) {
        fields.put(secondaryKeyPrefix + i, new ImmutablePair<>(valuePrefix + i, i + minTimeout));
        if (i % 100 == 0) {
            Assert.assertEquals("OK", client.ehmset(expireHashKey, fields));
            fields = new HashMap<>();
        }
    }

    int count = 0;
    String cursor = "0";
    do {
        ScanResult<Map.Entry<String, String>> values = client.ehscan(expireHashKey, cursor);
        count += values.getResult().size();
        cursor = values.getCursor();
    } while (cursor.compareTo("0") != 0);

    Assert.assertEquals(fieldCount, count);
}
 
Example 10
Source File: RedisIterator.java    From tutorials with MIT License 5 votes vote down vote up
@Override
public List<T> next() {
    if (cursor == null) {
        cursor = "0";
    }
    try (Jedis jedis = jedisPool.getResource()) {
        ScanResult<T> scanResult = strategy.scan(jedis, cursor, scanParams);
        cursor = scanResult.getCursor();
        return scanResult.getResult();

    } catch (Exception ex) {
        log.error("Exception caught in next()", ex);
    }
    return new LinkedList<T>();
}