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

The following examples show how to use redis.clients.jedis.ScanResult#getResult() . 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: 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 3
Source File: KeyTests.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void scanTest() {
    final String prefix = "scan.test-";
    final Map<String, Object> map = Maps.newHashMap();
    for (int idx = 0; idx < 100; idx++) {
        map.put(prefix + idx, idx);
    }

    redisClient.set(map);

    final ScanParams params = new ScanParams().match(prefix + '*');
    long cursor = -1;
    while (cursor == -1 || cursor > 0) {
        final ScanResult<String> res = redisClient.scan(cursor == -1 ? 0 : cursor, params);
        final String nextCursor = res.getStringCursor();
        cursor = Long.parseLong(nextCursor);
        final List<String> keys = res.getResult();
        LOGGER.debug("{}", keys);
        if (cursor > 0) {
            Assert.assertTrue(keys.size() > 0);
        }
    }

    redisClient.del(map.keySet().toArray(new String[map.size()]));
}
 
Example 4
Source File: RedisClusterClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public ScanResult<Entry<String, Double>> zscan(final String key, final long cursor, final ScanParams params) {
    Assert.hasText(key);
    Assert.notNull(params);

    try {
        final ScanResult<Tuple> res = cluster.zscan(key, String.valueOf(cursor), params);
        final List<Tuple> tuples = res.getResult();
        if (CollectionUtils.isEmpty(tuples)) {
            return new ScanResult<>(res.getStringCursor(), Collections.emptyList());
        }

        final List<Entry<String, Double>> newTuples = Lists.newArrayList();
        tuples.forEach(tuple -> newTuples.add(new AbstractMap.SimpleEntry<>(tuple.getElement(), tuple.getScore())));
        return new ScanResult<>(res.getStringCursor(), newTuples);
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    }
}
 
Example 5
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public ScanResult<Entry<String, Double>> zscan(final String key, final long cursor, final ScanParams params) {
    Assert.hasText(key);
    Assert.notNull(params);

    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final ScanResult<Tuple> res = jedis.zscan(key, String.valueOf(cursor), params);
        final List<Tuple> tuples = res.getResult();
        if (CollectionUtils.isEmpty(tuples)) {
            return new ScanResult<>(res.getStringCursor(), Collections.emptyList());
        }

        final List<Entry<String, Double>> newTuples = Lists.newArrayList();
        tuples.forEach(tuple -> newTuples.add(new AbstractMap.SimpleEntry<>(tuple.getElement(), tuple.getScore())));
        return new ScanResult<>(res.getStringCursor(), newTuples);
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example 6
Source File: AbstractRedisInputOperator.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
private void scanKeysFromOffset()
{
  if (!scanComplete) {
    if (replay && scanCallsInCurrentWindow >= recoveryState.numberOfScanCallsInWindow) {
      try {
        Thread.sleep(sleepTimeMillis);
      } catch (InterruptedException e) {
        DTThrowable.rethrow(e);
      }
      return;
    }

    ScanResult<String> result = store.ScanKeys(scanOffset, scanParameters);
    backupOffset = scanOffset;
    scanOffset = Integer.parseInt(result.getStringCursor());
    if (scanOffset == 0) {
      // Redis store returns 0 after all data is read
      scanComplete = true;

      // point scanOffset to the end in this case for reading any new tuples
      scanOffset = backupOffset + result.getResult().size();
    }
    keys = result.getResult();
  }
  scanCallsInCurrentWindow++;
}
 
Example 7
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 8
Source File: DynoProxy.java    From conductor with Apache License 2.0 6 votes vote down vote up
public Set<String> hkeys(String key) {
    logger.trace("hkeys {}", key);
    JedisCommands client = dynoClient;
    Set<String> keys = new HashSet<>();
    int cursor = 0;
    do {
        ScanResult<Entry<String, String>> sr = client.hscan(key, "" + cursor);
        cursor = Integer.parseInt(sr.getCursor());
        List<Entry<String, String>> result = sr.getResult();
        for (Entry<String, String> e : result) {
            keys.add(e.getKey());
        }
    } while (cursor > 0);

    return keys;
}
 
Example 9
Source File: DynoProxy.java    From conductor with Apache License 2.0 6 votes vote down vote up
public Map<String, String> hscan(String key, int count) {
    Map<String, String> m = new HashMap<>();
    int cursor = 0;
    do {
        ScanResult<Entry<String, String>> scanResult = dynoClient.hscan(key, "" + cursor);
        cursor = Integer.parseInt(scanResult.getCursor());
        for (Entry<String, String> r : scanResult.getResult()) {
            m.put(r.getKey(), r.getValue());
        }
        if (m.size() > count) {
            break;
        }
    } while (cursor > 0);

    return m;
}
 
Example 10
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 11
Source File: DynoProxy.java    From conductor with Apache License 2.0 5 votes vote down vote up
public Map<String, String> hgetAll(String key) {
    Map<String, String> m = new HashMap<>();
    JedisCommands dyno = dynoClient;
    int cursor = 0;
    do {
        ScanResult<Entry<String, String>> scanResult = dyno.hscan(key, "" + cursor);
        cursor = Integer.parseInt(scanResult.getCursor());
        for (Entry<String, String> r : scanResult.getResult()) {
            m.put(r.getKey(), r.getValue());
        }
    } while (cursor > 0);

    return m;
}
 
Example 12
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 13
Source File: AbstractRedisClient.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ScanResult<T> scan(final long cursor, final ScanParams params, final TypeReference<T> type) {
    final ScanResult<String> res = scan(cursor, params);
    final String nextCursor = res.getStringCursor();
    final long next = Long.parseLong(nextCursor);
    if (next == 0) {
        return new ScanResult<>(START_CURSOR, Collections.emptyList());
    }

    final List<String> values = res.getResult();
    final List<T> newValues = values.stream().map(value -> JSON.parseObject(value, type)).collect(Collectors.toList());
    return new ScanResult<>(nextCursor, newValues);
}
 
Example 14
Source File: AbstractRedisClient.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ScanResult<Entry<String, T>> hscan(final String key, long cursor, final ScanParams params, final TypeReference<T> type) {
    final ScanResult<Entry<String, String>> result = hscan(key, cursor, params);
    final List<Entry<String, String>> entrys = result.getResult();
    if (CollectionUtils.isEmpty(entrys)) {
        return new ScanResult<>(result.getStringCursor(), Collections.emptyList());
    }

    final List<Entry<String, T>> newEntrys = Lists.newArrayList();
    entrys.forEach(entry -> newEntrys.add(new AbstractMap.SimpleEntry<>(entry.getKey(), JSON.parseObject(entry.getValue(), type))));
    return new ScanResult<>(result.getStringCursor(), newEntrys);
}
 
Example 15
Source File: AbstractRedisClient.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ScanResult<T> sscan(final String key, final long cursor, final ScanParams params, final TypeReference<T> type) {
    final ScanResult<String> result = sscan(key, cursor, params);
    final List<String> values = result.getResult();
    if (CollectionUtils.isEmpty(values)) {
        return new ScanResult<>(result.getStringCursor(), Collections.emptyList());
    }

    final List<T> newValues = Lists.newArrayList();
    values.forEach(value -> newValues.add(JSON.parseObject(value, type)));
    return new ScanResult<>(result.getStringCursor(), newValues);
}
 
Example 16
Source File: AbstractRedisClient.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public <T> ScanResult<Entry<T, Double>> zscan(final String key, final long cursor, final ScanParams params, final TypeReference<T> type) {
    final ScanResult<Entry<String, Double>> result = zscan(key, cursor, params);
    final List<Entry<String, Double>> entrys = result.getResult();
    if (CollectionUtils.isEmpty(entrys)) {
        return new ScanResult<>(result.getStringCursor(), Collections.emptyList());
    }

    final List<Entry<T, Double>> newEntrys = Lists.newArrayList();
    entrys.forEach(entry -> newEntrys.add(new AbstractMap.SimpleEntry<>(JSON.parseObject(entry.getKey(), type), entry.getValue())));
    return new ScanResult<>(result.getStringCursor(), newEntrys);
}
 
Example 17
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 18
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>();
}
 
Example 19
Source File: AutoCommandResult.java    From redis-manager with Apache License 2.0 4 votes vote down vote up
public AutoCommandResult(ScanResult<String> scanResult) {
    this.value = scanResult.getResult();
}