本文是對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