Redis:Java链接redis单节点千万级别数据 写入,读取 性能测试

本文是对Redis 单节点,针对不同的数据类型,做插入行测试. 数据总条数为:10058624

环境说明:

            Redis 未做任何优化, 单节点    (服务器上, 内存64G).

            数据量 : 10058624条  (大约一千零6万条数据,本地机器运行读取插入操作.)

            数据大小 :  1093.56MB  (1.1G)

 插入数据类型为 String 类型

Jedis插入

public void save(){
    //连接本地Redis服务
    Jedis jedis = new Jedis("bj-rack001-hadoop006");
    InputStream fis = null;
    fis = new BufferedInputStream(new FileInputStream(PATH));
    //根据数据流初始化一个DBFReader实例,用来读取DBF文件信息
    DBFReader reader = new DBFReader(fis);

    Object[] rowValues;
    int index = 0;
    
    while ((rowValues = reader.nextRecord()) != null){
        if (null != rowValues && rowValues.length > 0) {
            index ++;
            if (index %10000 == 0) {
                long end = System.currentTimeMillis();
                System.out.println("处理数据中 ==> 插入数据总条数 : "+index+"  总耗时 : "+ (end-start)/1000 + " s , 处理速度 : " +(index/((end-start)/1000))+"  条 / s");
            }
            jedis.set("index" + SEPARATOR +index, Array2String(rowValues));
        }
    }
jedis.close(); }

假设插入速度为 2800条/s , 那么插入10058624 条数据需要用时: 3593秒 .  (  59.88 min , 约 1小时.  )!!!!!

Pipelining插入

public void save(){
    //连接本地Redis服务
    Jedis jedis = new Jedis("bj-rack001-hadoop006");
    Pipeline pipelined = jedis.pipelined();

    InputStream fis = null;
    fis = new BufferedInputStream(new FileInputStream(PATH));
    //根据数据流初始化一个DBFReader实例,用来读取DBF文件信息
    DBFReader reader = new DBFReader(fis);

    Object[] rowValues;
    int index = 0;
    
    while ((rowValues = reader.nextRecord()) != null){
        if (null != rowValues && rowValues.length > 0) {
            index ++;
            if (index %10000 == 0) {
                long end = System.currentTimeMillis();
                System.out.println("处理数据中 ==> 插入数据总条数 : "+index+"  总耗时 : "+ (end-start)/1000 + " s , 处理速度 : " +(index/((end-start)/1000))+"  条 / s");
            }
            pipelined.set("index"+ SEPARATOR +index,Array2String(rowValues));
        }
    }
    pipelined.sync();
    jedis.close();
}

处理数据完成  ==> 插入数据总条数 size : 10058624   total use : 62 s  , 处理速度: 162235 条/s

和传统方式相比,性能差将近58 倍!!!!!!!!

读取全部 String 类型数据

Jedis读取

public void save(){
    //连接本地Redis服务
    Jedis jedis = new Jedis("bj-rack001-hadoop006");
    long start = System.currentTimeMillis();

    int num = 10058624;    
    for (int index = 1; index < num; index++){
            if (index %10000 == 0) {
                long end = System.currentTimeMillis();
                System.out.println("处理数据中 ==> 插入数据总条数 : "+index+"  总耗时 : "+ (end-start)/1000 + " s , 处理速度 : " +(index/((end-start)/1000))+"  条 / s");
            }
            String key = KEYPREFIX + SEPARATOR + index;
            String value = jedis.get(key);
    }
    jedis.close();
}

假设插入速度为 5000条/s , 那么插入10058624 条数据需要用时: 2012 秒  .  (  33min 30 s  .  )

Pipelining读取

public void save(){
    //连接本地Redis服务
    Jedis jedis = new Jedis("bj-rack001-hadoop006");
    Pipeline pipelined = jedis.pipelined();

    long start = System.currentTimeMillis();
    HashMap<String, Response<String>> intrmMap = new HashMap<String, Response<String>>();

    int num = 10058624;    
    for (int index = 1; index < num; index++){
            if (index %10000 == 0) {
                long end = System.currentTimeMillis();
                System.out.println("处理数据中 ==> 插入数据总条数 : "+index+"  总耗时 : "+ (end-start)/1000 + " s , 处理速度 : " +(index/((end-start)/1000))+"  条 / s");
            }
            String key = KEYPREFIX + SEPARATOR + index;
            intrmMap.put(key, pipelined.get(key));
    }
    pipelined.sync();
    jedis.close();
}

由于是异步操作, 所以上面的结果并不准.最终获取数据时间

batchGetUsePipeline : 处理数据完成  ==> 读取数据总条数 size : 10058623   total use : 48 s  , 处理速度: 209554 条/s 

和传统方式相比,性能差将近 41.92  倍!!!!!!!!

插入数据类型为 List 类型

 Jedis插入

public void save(){
    //连接本地Redis服务
    Jedis jedis = new Jedis("bj-rack001-hadoop006");
    InputStream fis = null;
    fis = new BufferedInputStream(new FileInputStream(PATH));
    //根据数据流初始化一个DBFReader实例,用来读取DBF文件信息
    DBFReader reader = new DBFReader(fis);

    Object[] rowValues;
    int index = 0;
    
    while ((rowValues = reader.nextRecord()) != null){
        if (null != rowValues && rowValues.length > 0) {
            index ++;
            if (index %10000 == 0) {
                long end = System.currentTimeMillis();
                System.out.println("处理数据中 ==> 插入数据总条数 : "+index+"  总耗时 : "+ (end-start)/1000 + " s , 处理速度 : " +(index/((end-start)/1000))+"  条 / s");
            }
            jedis.lpush("index", JacksonUtils.toJSon(rowValues));
        }
    }
    jedis.close();
}

假设插入速度为 2600条/s , 那么插入10058624 条数据需要用时: 3869 秒 .  (  64.5 min , 约 1小时零5分钟.  )

Pipeline 插入

public void save(){
    //连接本地Redis服务
    Jedis jedis = new Jedis("bj-rack001-hadoop006");
    Pipeline pipelined = jedis.pipelined();

    InputStream fis = null;
    fis = new BufferedInputStream(new FileInputStream(PATH));
    //根据数据流初始化一个DBFReader实例,用来读取DBF文件信息
    DBFReader reader = new DBFReader(fis);

    Object[] rowValues;
    int index = 0;
    
    while ((rowValues = reader.nextRecord()) != null){
        if (null != rowValues && rowValues.length > 0) {
            index ++;
            if (index %10000 == 0) {
                long end = System.currentTimeMillis();
                System.out.println("处理数据中 ==> 插入数据总条数 : "+index+"  总耗时 : "+ (end-start)/1000 + " s , 处理速度 : " +(index/((end-start)/1000))+"  条 / s");
            }
            pipelined.lpush("index", JacksonUtils.toJSon(rowValues));
        }
    }
    pipelined.sync();
    jedis.close();
}

处理数据完成  ==> 插入数据总条数 size : 10058624   total use : 62 s  , 处理速度: 162235 条/s 

和传统方式对比  性能相差 62.5倍

读取全部 List 类型数据

Jedis读取

public void save(){
    //连接本地Redis服务
    Jedis jedis = new Jedis("bj-rack001-hadoop006");
    long start = System.currentTimeMillis();

   List<String> list = jedis.lrange("index", 0, 10058624);
   jedis.close();

   long end = System.currentTimeMillis();
   System.out.println("处理数据中 ==> 读取数据总条数 : "+list.size()+"  耗时 : "+ start  + " s , 处理速度 : " +(list.size()/((end-start)/1000))+"  条 / s");
}

读取数据总条数 size : 10058624   total use : 15 s  , 处理速度: 670574 条/s

Pipline读取

public void save(){
    //连接本地Redis服务
    Jedis jedis = new Jedis("bj-rack001-hadoop006");
    long start = System.currentTimeMillis();
    Pipeline pipelined = jedis.pipelined();

    Response<List<String>> list = pipelined.lrange("index", 0, 10058624);
   
    pipelined.sync();
    jedis.close();

    long end = System.currentTimeMillis();
    System.out.println("处理数据中 ==> 读取数据总条数 : "+list.size()+"  耗时 : "+ start  + " s , 处理速度 : " +(list.size()/((end-start)/1000))+"  条 / s");
}

处理数据完成  ==> 读取数据总条数 size : 10058624   total use : 12 s  , 处理速度: 838218 条/s

pipline的数据读取方式确实会快很多, 但是内存存在消耗

文章转载至:https://blog.csdn.net/zhanglong_4444/article/details/87921162

原文地址:https://www.cnblogs.com/nhdlb/p/14048746.html