redis模糊批量刪除key
阿新 • • 發佈:2018-11-06
1.shell方式
[[email protected] shell]# cat 2_batchDelRedisKeysByPattern.sh #!/bin/bash COMMOND_HOME=/usr/local/redis-4.0.10/bin/redis-cli pattern=$1 echo "batch del keys ${pattern}" # del stand-alone redis #redis-cli -h 127.0.0.1 -p 6379 -a [email protected]+. keys ${pattern} | xargs -i redis-cli -h 127.0.0.1 -p 6379 -a
[email protected]+. del {} #batch del redis cluster #1. fetch master nodes hostandport #172.27.0.13:12301 #172.27.0.13:12302 #172.27.0.13:12303 echo "1. fetch master nodes hostandport" #${COMMOND_HOME} -c -h 172.17.0.13 -p 12306 cluster nodes|grep master|awk '{print $2}'|awk -F "@" '{print $1}' >redis_cluster_nodes.txt #2. create HostAndPort array echo "2. create HostAndPort array" HostAndPort=() for line in $(cat redis_cluster_nodes.txt) do HostAndPort=(${HostAndPort[@]} ${line}) done #3. iter HostAndPort array to del keys by pattern echo "3. iter HostAndPort array to del keys by pattern" for node in ${HostAndPort[@]} do echo "start batch del ${node} keys ${pattern}" ip=`echo ${node}|cut -d : -f 1` port=`echo ${node}|cut -d : -f 2` ${COMMOND_HOME} -c -h ${ip} -p ${port} keys ${pattern}|xargs -i ${COMMOND_HOME} -c -h ${ip} -p ${port} del {} done echo "completed"
2.Jedis(java)
https://blog.csdn.net/xiao_jun_0820/article/details/77227104
public static void batchDeleteForCluster(String pattern) { JedisCluster cluster = JedisPoolUtil.getInstance().getCluster(); Map<String, JedisPool> clusterNodes = cluster.getClusterNodes(); for (Map.Entry<String, JedisPool> entry : clusterNodes.entrySet()) { jedis = ((JedisPool)entry.getValue()).getResource(); if (!jedis.info("replication").contains("role:slave")) { Set<String> keys = jedis.keys(pattern); if (keys.size() > 0) { Map<Integer, List<String>> map = new HashMap(6600); for (String key : keys) { int slot = JedisClusterCRC16.getSlot(key); if (map.containsKey(Integer.valueOf(slot))) { ((List)map.get(Integer.valueOf(slot))).add(key); } else { List<String> list = new ArrayList(); list.add(key); map.put(Integer.valueOf(slot), list); } } for (Map.Entry<Integer, List<String>> integerListEntry : map.entrySet()) { jedis.del((String[])((List)integerListEntry.getValue()).toArray(new String[((List)integerListEntry.getValue()).size()])); } } } } Jedis jedis; System.out.println("finished!"); }
非叢集方式
public static void batchDelete(String pattern, int count)
{
Jedis jedis = null;
ScanParams scanParams = new ScanParams();
scanParams.count(Integer.valueOf(count));
String scanRet = "0";
try
{
jedis = JedisPoolUtil.getInstance().getJedisFromPoll();
List<String> retList = new ArrayList();
do
{
retList.clear();
ScanResult ret = jedis.scan(scanRet, scanParams.match(pattern));
scanRet = ret.getStringCursor();
retList.addAll(ret.getResult());
Pipeline pipeline = jedis.pipelined();
pipeline.multi();
for (String key : retList) {
pipeline.del(key);
}
pipeline.exec();
pipeline.sync();
} while (!scanRet.equals("0"));
if (null != jedis) {
jedis.close();
}
}
catch (Exception e)
{
e.printStackTrace();
if (null != jedis) {
jedis.close();
}
}
finally
{
if (null != jedis) {
jedis.close();
}
}
System.out.println("finished!");
}