search

Home  >  Q&A  >  body text

redis - 如何用java实现基于JedisCluster对象的连接池?

今天做了一个基于redis集群的java小demo,用于从hadoop中读取文件,然后写入redis集群,但是单个JedisCluster连接在生产中不够效率,不知道哪位大神能给点基于JedisCluster的连接池示例?
下面是我的一点代码
`static {
    URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
}

public static void main(String[] args) throws Exception {
    writeRedis("hdfs://mycluster/test/test.txt",new int[]{0});
    //writeRedis(initConfig());
}
public static void writeRedis(String inpath, int[] keyIndex) throws Exception {
    InputStream in = new URL(inpath).openStream();// 输入流 ]
    LineIterator br = org.apache.commons.io.IOUtils.lineIterator(in, "utf8");// 指定输入流与字符编码
    JedisCluster cluster = RedisUtils.getClusterConn();
    String[] line = null;
    while (br.hasNext()) {
        String s = br.next();
        line = RedisUtils.split(s);
        String key = "";
        for (int i = 0; i < keyIndex.length; i++) {
            key += line[keyIndex[i]];// TODO:如果有必要,添加分隔符
        }
        cluster.setnx(key, s);
    }
    RedisUtils.clusterClose(cluster);
}`

拜谢!!!

伊谢尔伦伊谢尔伦2805 days ago810

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:07:46

    But use common-pool to encapsulate it yourself

    reply
    0
  • Cancelreply