redis批量删除key

 

1.生成key

192.168.1.50:6379> set name1 hxl
OK
192.168.1.50:6379> set name2 hxl02
OK
192.168.1.50:6379> set name3 hxl03
OK
192.168.1.50:6379> get name1
"hxl"
192.168.1.50:6379> get name2
"hxl02"
192.168.1.50:6379> get name3
"hxl03"
192.168.1.50:6379> exit

批量删除
[root@cdc-henan-cdhworker02 /]# /usr/local/redis/bin/redis-cli -h 192.168.1.50 -p 6379 -a test123 KEYS "*name*"|xargs /usr/local/redis/bin/redis-cli -h 192.168.1.50 -a test123 del
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(integer) 3

检查是否已经删除


[root@cdc-henan-cdhworker02 /]# /usr/local/redis/bin/redis-cli -h 192.168.1.50 -p 6379 -a test123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.1.50:6379> get name1
(nil)
192.168.1.50:6379> get name2
(nil)
192.168.1.50:6379> get name3
(nil)
192.168.1.50:6379>

 

2.批量删除(方法1)

2.1 导出key到文件
/usr/local/services/redis/bin/redis-cli -h 192.168.1.100 -p 6379 -a testtest KEYS "*STOCK*">/tmp/rediskey.txt
若对这些key进行其他的操作,比如del,可以在每行前面加上del,使用vi命令如下:

:%s/^/DEL /

 或是用sed实现

sed 's/^/DEL &/g' a.txt

 

这个时候生成的文件内容就是redis命令了,如下:

DEL digitalclinic-basedata:dubbo:hospitalInfo:hospitalId:3538
DEL digitalclinic-basedata:dubbo:hospitalInfo:hospitalId:3157
DEL digitalclinic-basedata:dubbo:hospitalInfo:hospitalId:23747
DEL digitalclinic-basedata:dubbo:hospitalInfo:hospitalId:24663
DEL digitalclinic-basedata:dubbo:hospitalInfo:hospitalId:15391
DEL digitalclinic-basedata:dubbo:hospitalInfo:hospitalId:24059

...


2.2 不采用管道方式:
cat /tmp/hospitalId_last.txt | /usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a 123456

采用管道方式:
cat /tmp/hospitalId_last.txt | /usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a 123456 --pipe

采用管道的方式需要将文件进行格式转换
[root@master ~]#yum install unix2dos
[root@master ~]#unix2dos hospitalId_last.txt

 

 

-- The End --

posted @ 2020-11-19 11:48  slnngk  阅读(594)  评论(0编辑  收藏  举报