Redis集群中添加数据,删除某个前缀开头的数据

maven pom.xml中添加相关的包

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.4.2</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

编写测试类:

package redis;

import redis.clients.jedis.*;
import java.util.*;

public class RedisUtilMain {

    //日志储存key的前缀
    private static final String REDIS_KEY_PREFIX = "Test:log:";
    public static void main(String[] args) {
        JedisCluster jedisCluster = getJedisCluster();
        //redis中添加数据
        for (int i=0;i<500;i++){
            jedisCluster.set(REDIS_KEY_PREFIX+UUID.randomUUID().toString(),UUID.randomUUID().toString());
            jedisCluster.set(REDIS_KEY_PREFIX+"partition:0:"+UUID.randomUUID().toString(),UUID.randomUUID().toString());
        }
        //只删除test:log:开头的不删除test:log:partition:开头的消息
        Set<String> allKeys = keys(REDIS_KEY_PREFIX +  "*", jedisCluster);
        System.out.println("key size:" + allKeys.size());
        for (String key:allKeys){
            if(!(key.contains(REDIS_KEY_PREFIX+ "partition:"))){
                jedisCluster.del(key);
                System.out.println(key);
            }
        }
    }
    /**
     * redis集群keys方法
     * @param pattern
     * @param jedisCluster
     * @return
     */
    public static TreeSet<String> keys(String pattern, JedisCluster jedisCluster){
        TreeSet<String> keys = new TreeSet<>();
        Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
        for(String k : clusterNodes.keySet()){
            JedisPool jp = clusterNodes.get(k);
            Jedis connection = jp.getResource();
            try {
                keys.addAll(connection.keys(pattern));
            } catch(Exception e){
            } finally{
                //logger.debug("Connection closed.");
                connection.close();
            }
        }
        return keys;
    }
    static JedisCluster getJedisCluster(){
        List<String> hostAndPortList = new ArrayList<>();

        hostAndPortList.add("192.168.1.147:7000");
        hostAndPortList.add("192.168.1.147:7001");
        hostAndPortList.add("192.168.1.148:7002");
        hostAndPortList.add("192.168.1.148:7003");
        hostAndPortList.add("192.168.1.149:7004");
        hostAndPortList.add("192.168.1.149:7005");
        int timeOut = 5000;
        int maxAttempts = 10000000;

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(10);
        config.setMaxIdle(10);
        config.setMinIdle(1);
        config.setMaxWaitMillis(20*1000);
        config.setTestOnBorrow(true);

        Set<HostAndPort> nodes = new LinkedHashSet<HostAndPort>();
        for (String hostAndPort : hostAndPortList) {
            String[] host_port = hostAndPort.split(":");
            String host = host_port[0];
            int port = Integer.parseInt(host_port[1]);
            nodes.add(new HostAndPort(host, port));
        }

        JedisCluster jedisCluster = null;
        if (timeOut > 0 && maxAttempts > 0){
            jedisCluster = new JedisCluster(nodes, timeOut, maxAttempts, config);
        }

        else{
            jedisCluster = new JedisCluster(nodes);
        }
        return jedisCluster;
    }

}

Redis集群中添加数据,删除某个前缀开头的数据