1. 程式人生 > 其它 >向redis中寫入大量的資料

向redis中寫入大量的資料

技術標籤:redis

往redis中寫入大量的資料
1.編寫Python指令碼,生成redis命令
redis_info.py

#!/usr/bin/python
for i in range(5000000): #迴圈的數量
        print 'set name'+str(i),'hello'+str(i) #str(i)將int型別轉換為str型別,否則不能進行字串拼接

2.執行python指令碼輸出到redis_comm.txt檔案中

python redis_info.py > redis_comm.txt

生成一個redis_comm.txt檔案

head -n 10 redis_comm.
txt set name0 hello0 set name1 hello1 set name2 hello2 set name3 hello3 set name4 hello4 set name5 hello5 set name6 hello6 set name7 hello7 set name8 hello8 set name9 hello9

3.將redis命令生成Redis Protocol
編寫指令碼redis_data.sh
#!/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_comm.txt

4.執行shell指令碼

sh redis_data.sh > redis_data.txt
head -n 10 redis_data.txt 
*3
$3
set
$5
name0
$6
hello0
*
3 $3 set

5.管道(Pipeline)就是為了改善這個情況的,利用管道技術,客戶端可以一次性發送多個請求而不用等待伺服器的響應,待所有命令都發送完後再一次性讀取服務的響應。

 cat redis_data.txt |redis-cli -h 192.168.102.95 --pipe

直接就可以運行了
6.驗證,去檢視資料是否插入成功

info檢視

在這裡插入圖片描述