redis.clients.jedis.ScanResult Java Examples

The following examples show how to use redis.clients.jedis.ScanResult. 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: EnhancedJedisCluster.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
@Override
public ScanResult<byte[]> scan(final byte[] cursor, final ScanParams params) {

	String matchPattern = null;

	if (params == null || (matchPattern = scanMatch(params)) == null || matchPattern.isEmpty()) {
		throw new IllegalArgumentException(
				BinaryJedisCluster.class.getSimpleName() + " only supports SCAN commands with non-empty MATCH patterns");
	}

	if (JedisClusterHashTagUtil.isClusterCompliantMatchPattern(matchPattern)) {

		return new EnhancedJedisClusterCommand<ScanResult<byte[]>>(connectionHandler, maxAttempts) {
			@Override
			public ScanResult<byte[]> doExecute(Jedis connection) {
				return connection.scan(cursor, params);
			}
		}.runBinary(SafeEncoder.encode(matchPattern));
	} else {
		throw new IllegalArgumentException(BinaryJedisCluster.class.getSimpleName()
				+ " only supports SCAN commands with MATCH patterns containing hash-tags ( curly-brackets enclosed strings )");
	}
}
 
Example #2
Source File: DynoProxy.java    From conductor with Apache License 2.0 6 votes vote down vote up
public Set<String> smembers(String key) {
    logger.trace("smembers {}", key);
    JedisCommands client = dynoClient;
    Set<String> r = new HashSet<>();
    int cursor = 0;
    ScanParams sp = new ScanParams();
    sp.count(50);

    do {
        ScanResult<String> scanResult = client.sscan(key, "" + cursor, sp);
        cursor = Integer.parseInt(scanResult.getCursor());
        r.addAll(scanResult.getResult());

    } while (cursor > 0);

    return r;

}
 
Example #3
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 #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: 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 #6
Source File: SortedSetTests.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Test
public void zscanTest() {
    final String key = "zscan.test";
    final String prefix = "zscan.test-";
    try {
        final Map<Object, Double> map = Maps.newHashMap();
        for (int idx = 0; idx < 1000; idx++) {
            map.put(prefix + idx, Double.valueOf(idx));
        }

        redisClient.zadd(key, map);
        final AtomicLong cursor = new AtomicLong(-1);
        final ScanParams params = new ScanParams().count(10);
        while (cursor.get() == -1 || cursor.get() > 0) {
            if (cursor.get() == -1) {
                cursor.set(0);
            }

            final ScanResult<Entry<String, Double>> res = redisClient.zscan(key, cursor.get(), params);
            cursor.set(Long.valueOf(res.getStringCursor()));
        }
    } finally {
        redisClient.del(key);
    }
}
 
Example #7
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 #8
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 #9
Source File: CacheScanUnit.java    From xian with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(UnitRequest msg, Handler<UnitResponse> handler) throws Exception {
    String pattern = msg.getArgMap().get("pattern").toString();
    int count = msg.get("count", Integer.class, THRESHOLD_VALUE);
    String cursor = msg.getArgMap().get("cursor").toString();
    CacheConfigBean cacheConfigBean = msg.get("cacheConfig", CacheConfigBean.class);
    JSONObject jsonObject = Redis.call(cacheConfigBean, jedis -> {
        JSONObject _jsonObject = new JSONObject();
        ScanParams params = new ScanParams().match(pattern).count(count);
        ScanResult<String> scans = jedis.scan(cursor, params);
        _jsonObject.put("cursor", scans.getStringCursor());// 如果服务器向客户端返回 0 的游标时则表示迭代结束
        _jsonObject.put("result", scans.getResult());
        return _jsonObject;
    });
    handler.handle(UnitResponse.createSuccess(jsonObject));
}
 
Example #10
Source File: AllKindOfValuesCommandsTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void scan() {
  jedis.set("b", "b");
  jedis.set("a", "a");

  ScanResult<String> result = jedis.scan(SCAN_POINTER_START);

  assertEquals(SCAN_POINTER_START, result.getCursor());
  assertFalse(result.getResult().isEmpty());

  // binary
  ScanResult<byte[]> bResult = jedis.scan(SCAN_POINTER_START_BINARY);

  assertArrayEquals(SCAN_POINTER_START_BINARY, bResult.getCursorAsBytes());
  assertFalse(bResult.getResult().isEmpty());
}
 
Example #11
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 #12
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 #13
Source File: TracingJedisCluster.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<Entry<byte[], byte[]>> hscan(byte[] key, byte[] cursor) {
  Span span = helper.buildSpan("hscan", key);
  span.setTag("cursor", Arrays.toString(cursor));
  try {
    return super.hscan(key, cursor);
  } catch (Exception e) {
    onError(e, span);
    throw e;
  } finally {
    span.finish();
  }
}
 
Example #14
Source File: TracingJedisCluster.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<Tuple> zscan(byte[] key, byte[] cursor, ScanParams params) {
  Span span = helper.buildSpan("zscan", key);
  span.setTag("cursor", Arrays.toString(cursor));
  span.setTag("params", TracingHelper.toString(params.getParams()));
  try {
    return super.zscan(key, cursor, params);
  } catch (Exception e) {
    onError(e, span);
    throw e;
  } finally {
    span.finish();
  }
}
 
Example #15
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<String> sscan(final String key, final long cursor, final ScanParams params) {
    Assert.hasText(key);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        return jedis.sscan(key, String.valueOf(cursor), params);
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #16
Source File: JedisMock.java    From conductor with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<String> sscan(String key, String cursor, ScanParams params) {
	try {
		org.rarefiedredis.redis.ScanResult<Set<String>> sr = redis.sscan(key, Long.valueOf(cursor), "count", "1000000");
		List<String> list = sr.results.stream().collect(Collectors.toList());
           ScanResult<String> result = new ScanResult<String>("0", list);
           return result;
       }
       catch (Exception e) {
           throw new JedisException(e);
       }
}
 
Example #17
Source File: TracingJedisCluster.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<byte[]> sscan(byte[] key, byte[] cursor) {
  Span span = helper.buildSpan("sscan", key);
  span.setTag("cursor", Arrays.toString(cursor));
  try {
    return super.sscan(key, cursor);
  } catch (Exception e) {
    onError(e, span);
    throw e;
  } finally {
    span.finish();
  }
}
 
Example #18
Source File: TracingJedisCluster.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<Entry<byte[], byte[]>> hscan(byte[] key, byte[] cursor, ScanParams params) {
  Span span = helper.buildSpan("hscan", key);
  span.setTag("cursor", Arrays.toString(cursor));
  span.setTag("params", TracingHelper.toString(params.getParams()));
  try {
    return super.hscan(key, cursor, params);
  } catch (Exception e) {
    onError(e, span);
    throw e;
  } finally {
    span.finish();
  }
}
 
Example #19
Source File: TracingJedisCluster.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<Tuple> zscan(byte[] key, byte[] cursor) {
  Span span = helper.buildSpan("zscan", key);
  span.setTag("cursor", Arrays.toString(cursor));
  try {
    return super.zscan(key, cursor);
  } catch (Exception e) {
    onError(e, span);
    throw e;
  } finally {
    span.finish();
  }
}
 
Example #20
Source File: JedisMock.java    From conductor with Apache License 2.0 5 votes vote down vote up
public ScanResult<Entry<String, String>> hscan(final String key, final String cursor) {
 	try {
         org.rarefiedredis.redis.ScanResult<Map<String, String>> mockr = redis.hscan(key, Long.valueOf(cursor), "count", "1000000");
         Map<String, String> results = mockr.results;
         List<Entry<String, String>> list = results.entrySet().stream().collect(Collectors.toList());
ScanResult<Entry<String, String>> result = new ScanResult<Entry<String, String>>("0", list);

         return result;
     }
     catch (Exception e) {
         throw new JedisException(e);
     }
 }
 
Example #21
Source File: TracingJedisCluster.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<Entry<String, String>> hscan(String key, String cursor) {
  Span span = helper.buildSpan("hscan", key);
  span.setTag("cursor", cursor);
  try {
    return super.hscan(key, cursor);
  } catch (Exception e) {
    onError(e, span);
    throw e;
  } finally {
    span.finish();
  }
}
 
Example #22
Source File: TracingJedisCluster.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<Entry<String, String>> hscan(String key, int cursor) {
  Span span = helper.buildSpan("hscan", key);
  span.setTag("cursor", cursor);
  try {
    return super.hscan(key, cursor);
  } catch (Exception e) {
    onError(e, span);
    throw e;
  } finally {
    span.finish();
  }
}
 
Example #23
Source File: RedisClusterClientImpl.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<Entry<String, String>> hscan(final String key, final long cursor, final ScanParams params) {
    Assert.hasText(key);
    Assert.notNull(params);
    try {
        return cluster.hscan(key, String.valueOf(cursor), params);
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    }
}
 
Example #24
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 #25
Source File: TracingJedisCluster.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<byte[]> sscan(byte[] key, byte[] cursor, ScanParams params) {
  Span span = helper.buildSpan("sscan", key);
  span.setTag("cursor", Arrays.toString(cursor));
  span.setTag("params", TracingHelper.toString(params.getParams()));
  try {
    return super.sscan(key, cursor, params);
  } catch (Exception e) {
    onError(e, span);
    throw e;
  } finally {
    span.finish();
  }
}
 
Example #26
Source File: JedisMock.java    From conductor with Apache License 2.0 5 votes vote down vote up
public ScanResult<Tuple> zscan(final String key, final String cursor) {
try {
	org.rarefiedredis.redis.ScanResult<Set<ZsetPair>> sr = redis.zscan(key, Long.valueOf(cursor), "count", "1000000");
	List<ZsetPair> list = sr.results.stream().collect(Collectors.toList());
	List<Tuple> tl = new LinkedList<Tuple>();
	list.forEach(p -> tl.add(new Tuple(p.member, p.score)));
	ScanResult<Tuple> result = new ScanResult<Tuple>("0", tl);
          return result;
      }
      catch (Exception e) {
          throw new JedisException(e);
      }
  }
 
Example #27
Source File: JedisMock.java    From conductor with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<String> sscan(String key, String cursor, ScanParams params) {
	try {
		org.rarefiedredis.redis.ScanResult<Set<String>> sr = redis.sscan(key, Long.valueOf(cursor), "count", "1000000");
		List<String> list = sr.results.stream().collect(Collectors.toList());
           ScanResult<String> result = new ScanResult<String>("0", list);
           return result;
       }
       catch (Exception e) {
           throw new JedisException(e);
       }
}
 
Example #28
Source File: TracingJedisCluster.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<Entry<byte[], byte[]>> hscan(byte[] key, byte[] cursor) {
  Span span = helper.buildSpan("hscan", key);
  span.setTag("cursor", Arrays.toString(cursor));
  try {
    return super.hscan(key, cursor);
  } catch (Exception e) {
    onError(e, span);
    throw e;
  } finally {
    span.finish();
  }
}
 
Example #29
Source File: TracingJedisCluster.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<Tuple> zscan(String key, String cursor) {
  Span span = helper.buildSpan("zscan", key);
  span.setTag("cursor", cursor);
  try {
    return super.zscan(key, cursor);
  } catch (Exception e) {
    onError(e, span);
    throw e;
  } finally {
    span.finish();
  }
}
 
Example #30
Source File: TracingJedisCluster.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Override
public ScanResult<String> sscan(String key, String cursor) {
  Span span = helper.buildSpan("sscan", key);
  span.setTag("cursor", cursor);
  try {
    return super.sscan(key, cursor);
  } catch (Exception e) {
    onError(e, span);
    throw e;
  } finally {
    span.finish();
  }
}