美文网首页
JAVA使用pipeline批量写Redis数据

JAVA使用pipeline批量写Redis数据

作者: chuan_bai | 来源:发表于2018-01-27 17:30 被阅读1062次

最近遇到一个需求,需要把数据库中的手机号批量写入到数据库,使用了很多的方法都性能不佳或者出现连接池断开的问题,最后在网上找到了这个方法

    public static void main(String[] args) throws Exception {
        //连接redis
        Jedis redis = new Jedis("127.0.0.1", 6380, 400000);
        redis.auth("123456");

        Map<String, String> data = new HashMap<String, String>();
        redis.select(0);
        redis.flushDB();
        //hmset
        long start = System.currentTimeMillis();
        //直接hmset
        for (int i = 0; i < 10000; i++) {
            data.clear();
            data.put("k_" + i, "v_" + i);
            redis.hmset("key_" + i, data);
        }
        long end = System.currentTimeMillis();
        System.out.println("dbsize:[" + redis.dbSize() + "] .. ");
        System.out.println("hmset without pipeline used [" + (end - start) / 1000 + "] seconds ..");
        redis.select(0);
        redis.flushDB();
        //使用pipeline hmset
        Pipeline p = redis.pipelined();
        start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            data.clear();
            data.put("k_" + i, "v_" + i);
            p.hmset("key_" + i, data);
        }
        p.sync();
        end = System.currentTimeMillis();
        System.out.println("dbsize:[" + redis.dbSize() + "] .. ");
        System.out.println("hmset with pipeline used [" + (end - start) / 1000 + "] seconds ..");
        //hmget
        Set keys = redis.keys("*");
        //直接使用Jedis hgetall
        start = System.currentTimeMillis();
        Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>>();
        for (Object key : keys) {
            result.put((String) key, redis.hgetAll((String) key));
        }
        end = System.currentTimeMillis();
        System.out.println("result size:[" + result.size() + "] ..");
        System.out.println("hgetAll without pipeline used [" + (end - start) / 1000 + "] seconds ..");
        //使用pipeline hgetall
        Map<String, Response<Map<String, String>>> responses = new HashMap<String, Response<Map<String, String>>>(keys.size());
        result.clear();
        start = System.currentTimeMillis();
        for (Object key : keys) {
            responses.put((String) key, p.hgetAll((String) key));
        }
        p.sync();
        for (String k : responses.keySet()) {
            result.put(k, responses.get(k).get());
        }
        end = System.currentTimeMillis();
        System.out.println("result size:[" + result.size() + "] ..");
        System.out.println("hgetAll with pipeline used [" + (end - start) / 1000 + "] seconds ..");
        redis.disconnect();
    }

相关文章

网友评论

      本文标题:JAVA使用pipeline批量写Redis数据

      本文链接:https://www.haomeiwen.com/subject/veznaxtx.html