最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2016年11月23日 14:16:32   作者:iVictor  
本篇文章主要介紹了如何高效地向Redis插入大量的數(shù)據(jù),現(xiàn)在分享給大家,感興趣的小伙伴們可以參考一下。

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

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

今天,查看Redis官檔,發(fā)現(xiàn)文檔的首頁部分(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客戶端命令之間有往返時(shí)延。

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

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

那么如何高效的插入呢?

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

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

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

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

如果有了原始數(shù)據(jù),其實(shí)構(gòu)造這個文件并不難,譬如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

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

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

Shell

腳本如下:

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

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

Redis pipe

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

1> 首先構(gòu)造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> 將這些命令轉(zhuǎn)化成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

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

測試結(jié)果

如下:

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

最后,來看看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.

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

總結(jié):

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

[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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

  • redis計(jì)數(shù)器與數(shù)量控制的實(shí)現(xiàn)

    redis計(jì)數(shù)器與數(shù)量控制的實(shí)現(xiàn)

    使用Redis計(jì)數(shù)器可以輕松地解決數(shù)量控制的問題,同時(shí)還能有效地提高應(yīng)用的性能,本文主要介紹了redis計(jì)數(shù)器與數(shù)量控制的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • 關(guān)于Redis數(shù)據(jù)庫入門詳細(xì)介紹

    關(guān)于Redis數(shù)據(jù)庫入門詳細(xì)介紹

    大家好,本篇文章主要講的是關(guān)于Redis數(shù)據(jù)庫入門詳細(xì)介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • Redis基本數(shù)據(jù)類型哈希Hash常用操作命令

    Redis基本數(shù)據(jù)類型哈希Hash常用操作命令

    這篇文章主要為大家介紹了Redis基本數(shù)據(jù)類型哈希Hash常用操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • redis中zSet實(shí)現(xiàn)排行榜的使用示例

    redis中zSet實(shí)現(xiàn)排行榜的使用示例

    在工作中,有時(shí)候需要實(shí)現(xiàn)排行榜功能,本文主要介紹了redis中zSet實(shí)現(xiàn)排行榜的使用示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • redis在Linux系統(tǒng)下的環(huán)境配置和redis的全局命令大全

    redis在Linux系統(tǒng)下的環(huán)境配置和redis的全局命令大全

    在Linux系統(tǒng)中我們經(jīng)常使用Redis作為高性能的緩存數(shù)據(jù)庫,然而有時(shí)候我們需要在系統(tǒng)中多個地方使用Redis命令,這就需要將Redis的全局命令設(shè)置好,這篇文章主要給大家介紹了關(guān)于redis在Linux系統(tǒng)下的環(huán)境配置和redis的全局命令大全的相關(guān)資料,需要的朋友可以參考下
    2024-05-05
  • 淺談Redis在分布式系統(tǒng)中的協(xié)調(diào)性運(yùn)用

    淺談Redis在分布式系統(tǒng)中的協(xié)調(diào)性運(yùn)用

    這篇文章主要介紹了Redis在分布式系統(tǒng)中的協(xié)調(diào)性運(yùn)用,講解了Redis在進(jìn)程和線程的調(diào)度上以及消息隊(duì)列中的作用,需要的朋友可以參考下
    2016-03-03
  • Redis 哨兵與集群腦裂問題及其解決

    Redis 哨兵與集群腦裂問題及其解決

    本文主要介紹了Redis 哨兵與集群腦裂問題及其解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-04-04
  • Redis源碼與設(shè)計(jì)剖析之網(wǎng)絡(luò)連接庫

    Redis源碼與設(shè)計(jì)剖析之網(wǎng)絡(luò)連接庫

    這篇文章主要為大家介紹了Redis源碼與設(shè)計(jì)剖析之網(wǎng)絡(luò)連接庫詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Redis動態(tài)字符串SDS的實(shí)現(xiàn)

    Redis動態(tài)字符串SDS的實(shí)現(xiàn)

    SDS在Redis中是實(shí)現(xiàn)字符串對象的工具,本文主要介紹了Redis動態(tài)字符串SDS的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-11-11
  • Redis實(shí)現(xiàn)限量優(yōu)惠券的秒殺功能

    Redis實(shí)現(xiàn)限量優(yōu)惠券的秒殺功能

    文章詳細(xì)分析了避免超賣問題的方法,包括確保一人一單的業(yè)務(wù)邏輯,并提供了代碼實(shí)現(xiàn)步驟和代碼示例,感興趣的朋友跟隨小編一起看看吧
    2024-12-12

最新評論

灌南县| 拉孜县| 青田县| 南京市| 梧州市| 台前县| 德阳市| 泸水县| 剑川县| 承德市| 五华县| 高青县| 内乡县| 安丘市| 诸城市| 屏东县| 独山县| 怀化市| 铅山县| 铁力市| 廊坊市| 安西县| 丹凤县| 泸州市| 青阳县| 五寨县| 遂平县| 涞源县| 百色市| 朝阳县| 光山县| 通化县| 郓城县| 曲阳县| 奉贤区| 株洲县| 宝坻区| 屏南县| 航空| 平南县| 鸡西市|