麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 數(shù)據(jù)庫(kù) > Redis > 正文

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

2020-03-17 12:40:04
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最近有個(gè)哥們?cè)谌豪飭?wèn),有一個(gè)日志,里面存的是IP地址(一行一個(gè)),如何將這些IP快速導(dǎo)入到Redis中。

我剛開(kāi)始的建議是Shell+redis客戶端。

今天,查看Redis官檔,發(fā)現(xiàn)文檔的首頁(yè)部分(http://www.redis.io/documentation)有一個(gè)專(zhuān)門(mén)的主題是講述“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> 每個(gè)redis客戶端命令之間有往返時(shí)延。

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

個(gè)人理解是,redis命令從執(zhí)行到結(jié)果返回,有一定的時(shí)延,即便采用多個(gè)redis客戶單并發(fā)插入,也很難提高吞吐量,因?yàn)椋挥蟹亲枞鸌/O只能針對(duì)有限個(gè)連接操作。

那么如何高效的插入呢?

官方在2.6版本推出了一個(gè)新的功能-pipe mode,即將支持Redis協(xié)議的文本文件直接通過(guò)pipe導(dǎo)入到服務(wù)端。

說(shuō)來(lái)拗口,具體實(shí)現(xiàn)步驟如下:

1. 新建一個(gè)文本文件,包含redis命令

SET Key0 Value0SET Key1 Value1...SET KeyN ValueN

如果有了原始數(shù)據(jù),其實(shí)構(gòu)造這個(gè)文件并不難,譬如shell,python都可以

2. 將這些命令轉(zhuǎn)化成Redis Protocol。

因?yàn)镽edis管道功能支持的是Redis Protocol,而不是直接的Redis命令。

如何轉(zhuǎn)化,可參考后面的腳本

3. 利用管道插入

cat data.txt | redis-cli --pipe

Shell VS Redis pipe

下面通過(guò)測(cè)試來(lái)具體看看Shell批量導(dǎo)入和Redis pipe之間的效率。

測(cè)試思路:分別通過(guò)shell腳本和Redis pipe向數(shù)據(jù)庫(kù)中插入10萬(wàn)相同數(shù)據(jù),查看各自所花費(fèi)的時(shí)間。

Shell

腳本如下:

#!/bin/bashfor ((i=0;i<100000;i++))doecho -en "helloworld" | redis-cli -x set name$i >>redis.logdone

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

Redis pipe

Redis pipe會(huì)稍微麻煩一點(diǎn)

1> 首先構(gòu)造redis命令的文本文件

在這里,我選用了python

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

# python 1.py > redis_commands.txt

# head -2 redis_commands.txt

set name0 helloworldset name1 helloworld

2> 將這些命令轉(zhuǎn)化成Redis Protocol

在這里,我利用了github上一個(gè)shell腳本,

#!/bin/bashwhile 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"; donedone < redis_commands.txt

# sh 20.sh > redis_data.txt

# head -7 redis_data.txt

*3$3set$5name0$10helloworld

至此,數(shù)據(jù)構(gòu)造完畢。

測(cè)試結(jié)果

如下:
向redis寫(xiě)入大量數(shù)據(jù),redis,大量數(shù)據(jù)

時(shí)間消耗完全不是一個(gè)量級(jí)的。

最后,來(lái)看看pipe的實(shí)現(xiàn)原理,

  • 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.

即它會(huì)盡可能快的將數(shù)據(jù)發(fā)送到Redis服務(wù)端,并盡可能快的讀取并解析數(shù)據(jù)文件中的內(nèi)容,一旦數(shù)據(jù)文件中的內(nèi)容讀取完了,它會(huì)發(fā)送一個(gè)帶有20個(gè)字節(jié)的字符串的echo命令,Redis服務(wù)端即根據(jù)此命令來(lái)確認(rèn)數(shù)據(jù)已插入完畢。

總結(jié):

后續(xù)有童鞋好奇,構(gòu)造redis命令的時(shí)間和將命令轉(zhuǎn)化為protocol的時(shí)間,這里一并貼下:

[root@mysql-server1 ~]# time python 1.py > redis_commands.txtreal  0m0.110suser  0m0.070ssys  0m0.040s[root@mysql-server1 ~]# time sh 20.sh > redis_data.txtreal  0m7.112suser  0m5.861ssys  0m1.255s

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Redis頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 青青草成人av | 在线播放免费av | 国产91在线高潮白浆在线观看 | 亚洲性生活视频 | 国产精品视频成人 | 免费看真人a一级毛片 | 日本道中文字幕 | 亚洲精品一区国产精品丝瓜 | 国产免费人做人爱午夜视频 | 视频一区二区三区视频 | 中文区永久区 | 中文黄色一级片 | 国产一级一级 | 成人免费视频视频在线观看 免费 | 一级电影在线观看 | 欧美一区二区三区免费不卡 | 毛片在线免费观看网址 | 久久精品com | 国产在线地址 | 超污视频在线看 | 一区二区久久久久草草 | 吾色视频 | 欧美成人亚洲 | 久久草在线观看视频 | 成人黄色短视频在线观看 | 黄色片免费看网站 | a级毛片免费观看在线播放 日本aaa一级片 | 色婷婷a v | 黄色羞羞视频在线观看 | 中文国产在线视频 | 中文字幕国产欧美 | 奶子吧naiziba.cc免费午夜片在线观看 | 国产成年人小视频 | 欧美一极视频 | 日韩欧美电影一区二区三区 | 国产午夜精品久久久 | 在线播放一级片 | 久久骚| 久久免费观看一级毛片 | 日韩黄色片免费看 | 最新中文字幕免费视频 |