好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > 如何高效地向Redis插入大量的數據(推薦)

如何高效地向Redis插入大量的數據(推薦)

熱門標簽:竹間科技AI電銷機器人 西藏教育智能外呼系統價格 地圖標注如何即時生效 地圖標注費用 最簡單的百度地圖標注 小紅書怎么地圖標注店 百度商家地圖標注怎么做 玄武湖地圖標注 太原營銷外呼系統

最近有個哥們在群里問,有一個日志,里面存的是IP地址(一行一個),如何將這些IP快速導入到Redis中。

我剛開始的建議是Shell+redis客戶端。

今天,查看Redis官檔,發現文檔的首頁部分(http://www.redis.io/documentation)有一個專門的主題是講述“Redis Mass Insertion”的,才知道自己的建議很low。

官方給出的理由如下:

Using a normal Redis client to perform mass insertion is not a good idea for a few reasons: the naive approach of sending one command after the other is slow because you have to pay for the round trip time for every command. It is possible to use pipelining, but for mass insertion of many records you need to write new commands while you read replies at the same time to make sure you are inserting as fast as possible.

Only a small percentage of clients support non-blocking I/O, and not all the clients are able to parse the replies in an efficient way in order to maximize throughput. For all this reasons the preferred way to mass import data into Redis is to generate a text file containing the Redis protocol, in raw format, in order to call the commands needed to insert the required data.

大意是:

1> 每個redis客戶端命令之間有往返時延。

2> 只要一部分客戶端支持非阻塞I/O。

個人理解是,redis命令從執行到結果返回,有一定的時延,即便采用多個redis客戶單并發插入,也很難提高吞吐量,因為,只有非阻塞I/O只能針對有限個連接操作。

那么如何高效的插入呢?

官方在2.6版本推出了一個新的功能-pipe mode,即將支持Redis協議的文本文件直接通過pipe導入到服務端。

說來拗口,具體實現步驟如下:

1. 新建一個文本文件,包含redis命令

SET Key0 Value0
SET Key1 Value1
...
SET KeyN ValueN

如果有了原始數據,其實構造這個文件并不難,譬如shell,python都可以

2. 將這些命令轉化成Redis Protocol。

因為Redis管道功能支持的是Redis Protocol,而不是直接的Redis命令。

如何轉化,可參考后面的腳本。

3. 利用管道插入

cat data.txt | redis-cli --pipe

Shell VS Redis pipe

下面通過測試來具體看看Shell批量導入和Redis pipe之間的效率。

測試思路:分別通過shell腳本和Redis pipe向數據庫中插入10萬相同數據,查看各自所花費的時間。

Shell

腳本如下:

#!/bin/bash
for ((i=0;i100000;i++))
do
echo -en "helloworld" | redis-cli -x set name$i >>redis.log
done

每次插入的值都是helloworld,但鍵不同,name0,name1...name99999。

Redis pipe

Redis pipe會稍微麻煩一點

1> 首先構造redis命令的文本文件

在這里,我選用了python

#!/usr/bin/python
for i in range(100000):
  print 'set name'+str(i),'helloworld'

# python 1.py > redis_commands.txt

# head -2 redis_commands.txt

set name0 helloworld
set name1 helloworld

2> 將這些命令轉化成Redis Protocol

在這里,我利用了github上一個shell腳本,

#!/bin/bash

while read CMD; do
 # each command begins with *{number arguments in command}\r\n
 XS=($CMD); printf "*${#XS[@]}\r\n"
 # for each argument, we append ${length}\r\n{argument}\r\n
 for X in $CMD; do printf "\$${#X}\r\n$X\r\n"; done
done  redis_commands.txt

# sh 20.sh > redis_data.txt

# head -7 redis_data.txt

*3
$3
set
$5
name0
$10
helloworld

至此,數據構造完畢。

測試結果

如下:

時間消耗完全不是一個量級的。

最后,來看看pipe的實現原理,

  • redis-cli --pipe tries to send data as fast as possible to the server.
  • At the same time it reads data when available, trying to parse it.
  • Once there is no more data to read from stdin, it sends a special ECHO command with a random 20 bytes string: we are sure this is the latest command sent, and we are sure we can match the reply checking if we receive the same 20 bytes as a bulk reply.
  • Once this special final command is sent, the code receiving replies starts to match replies with this 20 bytes. When the matching reply is reached it can exit with success.

即它會盡可能快的將數據發送到Redis服務端,并盡可能快的讀取并解析數據文件中的內容,一旦數據文件中的內容讀取完了,它會發送一個帶有20個字節的字符串的echo命令,Redis服務端即根據此命令來確認數據已插入完畢。

總結:

后續有童鞋好奇,構造redis命令的時間和將命令轉化為protocol的時間,這里一并貼下:

[root@mysql-server1 ~]# time python 1.py > redis_commands.txt

real  0m0.110s
user  0m0.070s
sys  0m0.040s
[root@mysql-server1 ~]# time sh 20.sh > redis_data.txt

real  0m7.112s
user  0m5.861s
sys  0m1.255s

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • Redis批量刪除KEY的方法

標簽:林芝 贛州 廣東 澳門 揚州 唐山 香港 景德鎮

巨人網絡通訊聲明:本文標題《如何高效地向Redis插入大量的數據(推薦)》,本文關鍵詞  如何,高效,地,向,Redis,插入,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《如何高效地向Redis插入大量的數據(推薦)》相關的同類信息!
  • 本頁收集關于如何高效地向Redis插入大量的數據(推薦)的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 男女深夜福利| 久久国产精品亚洲AV七区色| 国模冰冰全套高清炮交图2| 国产农村真实bbwbbwbbw| 欧美顶级少妇做爰XXOO胖妞| 亚洲欧美中文无码字幕夜色在线| 亚洲成肉网| 草蜢视频在线观看免费版| 工口里番H全彩肉啪啪土豪| 国产精品乱码毛片在线人与| 18美女腿打开无遮软件| 欧美老妇人v片| 樱花动漫 官方入门网站| 大龟慢慢挺进女友闺蜜的小说| 二区中文字幕| 男人吃什么补得快| 性free非洲老妇| 中文字幕在线亚洲精品一区| tube性欧美第一次| 只恋你的床| 精品卡1卡2卡三卡免费网站| 久久精品国产亚洲一区二区三区| 在线播放一级片| 男人吃奶揉搓奶动态图| k8经典视频| 国模精品视频一区二区| 久久久久人妻精品一区二区红楼梦| 蜜桃社尤物馆美女图片| 空少被机长狂躁俩小时的故事| 女人被爽到呻吟gif动态图片| 强行扒开双腿进入| 国产精品调教视频| 外国人黄色片| 男生摸女生的胸视频| 性中国妇女熟女XXXX毛多| 久久久亚洲熟妇熟女ⅩXXX图| 亚洲AV欧洲AV人人爽爽蜜桃 | 久久精品亚洲AV无码四区大桥| 国产精品 27P| 张攸雨大胆两腿玉门打开图片| 国模无水印大尺度gogo|