向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查看

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/h111121111111/article/details/113726193
今日推荐