jedis的hscan方法使用方法(java)
解决方法:
1.hscan核心方法:
public Map<String, Integer> getAll(String hashKey, int iterSize) {
Jedis jedis = null;
Map<String, Integer> mapList=new HashMap<String, Integer>();
try {
int cursor = 0;
ScanParams scanParams = new ScanParams().match("*").count(iterSize > 0 ? iterSize : 1000);
ScanResult<Entry<String, String>> scanResult;
jedis = Redis.getJedis();
jedis.select(0);
do {
scanResult = jedis.hscan(hashKey, String.valueOf(cursor), scanParams);
for (Entry<String, String> en : scanResult.getResult()) {
//业务代码
}
//获取游标位置,若大于0,则代表还有数据,需要继续迭代
cursor = Integer.parseInt(scanResult.getStringCursor());
} while (cursor > 0);
}catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
return null;
}finally{
// 返还到连接池
if (jedis != null) {
jedis.close();
}
}
return mapList;
}
2.使用方法
Map<String, Integer> delHash = getAll("rd_sms_global_blacklist", 1000);