学习Redis之Redis 大量数据插入

1.创建个命令文件,保存为redis_commands.txt文件内容如下

 set key1 value1

 set key2 value2

 ...

2.由于redis的管道命令需要特定格式的数据流,所以要将redis_commands.txt的文件内容转一下,这里参考了一个脚本cre_rediscommands.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_commands.txt
复制代码
3.执行脚本将命令转为redis客户端可识别的命令生成文件redis_data.txt

[root@localhost bin]# sh cre_rediscommands.sh > redis_data.txt
4.执行管道命令,将redis_data.txt中的命令传输到redis-cli 中

[root@localhost bin]# cat redis_data.txt | redis-cli --pipe
5.登陆客户端
[root@localhost bin]# ./redis-cli 
6.查看执行结果

redis 127.0.0.1:6379> get key1
7.参考链接:

http://www.cnblogs.com/ivictor/p/5446503.html

http://www.redis.cn/topics/mass-insert.html





你可能感兴趣的:(LINUX)