redis-cli 通过管道 --pipe 快速导入数据到redis中

最近有个需求,需要把五千万条数据批量写入redis中,方法倒是有很多种!效率最高的就是通过redis-cl管道的方式写入

一:先看看命令

cat redis.txt | redis-cli -h 127.0.0.1 -a password - p 6379 --pipe

结构很简单 redis.txt 是你的文件名称 后面是你的客户端

二:redis.txt 的 文件格式

2.1 redis.txt的格式一

有两种文件格式 都是可以的

eg: set yutes testsss

*3

$3

set

$5

yutes

$7

testsss

*3

$3

set

$3

ymy

$9

testvalue

解释:

eg:

*3 表示有三个字符

$3 表示 set字符长度为3 也就是我们的命令

$5表示 yutes的长度为5 也就是我们的key

$7表示 testsss的长度为7 也就是我们的value

不同的字符直接 需要用换行隔开 例如紧接着下一个是 set ymy testvalue

另外一种方式 就比较简单直观

2.2 redis.txt的格式二

set yutes testsss

set ymy testvalue

把需要的自己这样写就行

三:文件格式转码

unix2dos redis.txt

上面的命令会去掉行尾的^M符号

四:执行命令就可以了 如图

四:总结

导入3250万数据大概用了 1分30秒左右

而这种方式 用了30多分钟!

遇到的坑

1: 没有 unix2dos 命令

apt install unix2dos 没有成功 提示 找不到 unix2dos pkg

更新 apt ,sudo apt update ,但是仍然找不到 unix2dos

添加 apt的源

vim /etc/apt/sources.list

可以参考链接 https://www.cnblogs.com/webnote/p/5767853.html

我的list为:

deb http://archive.ubuntu.com/ubuntu bionic main

deb http://archive.ubuntu.com/ubuntu bionic-security main

deb http://archive.ubuntu.com/ubuntu bionic-updates main

deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse

然后在重新sudo apt update 在 apt install unix2dos 或者 sudo apt-get install tofrodos

2: 上面的文件格式转码是

是 unix2dos filename 而不是 dos2unix filename

3: 集群模式下 如何写入

如果不通过 pipe 我们可以通过 redis-cli -c 来启动redis集群模式

cat redis.txt | redis-cli -c -h 127.0.0.1 -a password - p 6379

但是通过 --pipe 来启动集群模式的话 会 提示 MOVEN *** 172.20.**** 错误

那么解决方案有两种

一:暴力型的

你在集群模式下 --pipe 你会发现 key值的redis槽点在 此节点上 就写入成功了 不在此节点 就没有写入成功

那我们可以把每个节点跑 --pipe 一次,那么每个节点 就会写入自己的数据

二:细致型

通过key 的哈希值 区分槽点,通过节点拿到槽点。

沈阳生殖器疱疹医院:http://www.sdjk99.com/

沈阳性病医院:http://yiyuan.120ask.com/syxb/

猜你喜欢

转载自blog.csdn.net/a13804947436/article/details/84421677