当前位置:网站首页>Redis data migration method III
Redis data migration method III
2022-07-19 03:53:00 【Lao Wang's notes】
This method is written Shell Script , Get... One by one key, according to KEY Different types one by one KEY transfer , Slow speed , It is not recommended to use in large quantities
#!/bin/bash
# Address
host='127.0.0.1'
# Source port
src_port=7000
# Target port
dest_port=8000
# Counter , Count the number of migrations KEY, Print at the end
cnt=0
# Traverse to get all KEY
for k in `redis-cli -h $host -p $src_port keys "*"`
do
data_type=`redis-cli -h $host -p $src_port type $k`
# Judge one by one KEY After the type of , take KEY Read it out , Write to target Redis
case $data_type in
'string')
v=`redis-cli -h $host -p $src_port get $k`
redis-cli -h $host -p $dest_port set $k $v
;;
'list')
values=`redis-cli -h $host -p $src_port lrange $k 0 -1`
redis-cli -h $host -p $dest_port lpush $k $values
;;
'set')
values=`redis-cli -h $host -p $src_port smembers $k`
redis-cli -h $host -p $dest_port sadd $k $values
;;
'hash')
for key in `redis-cli -h $host -p $src_port hkeys $k`
do
hvalue=`redis-cli -h $host -p $src_port hget $k $key`
redis-cli -h $host -p $dest_port hset $k $key $hvalue
done
;;
*)
echo "not known type"
;;
esac
let cnt++
done
# Print migrated KEY
echo $cnt
The implementation of
bak]# sh m.sh
OK
(integer) 1
(integer) 1
(integer) 1
OK
OK
OK
(integer) 3
OK
OK
(integer) 1
OK
(integer) 5
OK
OK
(integer) 1
(integer) 1
OK
15
The results verify that
bak]# redis-cli -h 127.0.0.1 -p 8000 keys '*'
1) "k2"
2) "sname"
3) "k10"
4) "k4"
5) "hclass1"
6) "k8"
7) "k9"
8) "hclass2"
9) "k3"
10) "k6"
11) "lname"
12) "hclass3"
13) "k1"
14) "k7"
15) "k5"
notes : Online reference python Script , Because I execute python Code error , Manual change to shell Script ;
If you are interested, please refer to :
#coding=utf-8
import redis
redis_from = redis.StrictRedis(host='127.0.0.1', port=6379, db=10)
redis_to = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
if __name__ == '__main__':
cnt = 0
for k in redis_from.keys():
data_type = redis_from.type(k)
if data_type == 'string':
v = redis_from.get(k)
redis_to.set(k, v)
elif data_type == 'list':
values = redis_from.lrange(k, 0, -1)
redis_to.lpush(k, values)
elif data_type == 'set':
values = redis_from.smembers(k)
redis_to.sadd(k, values)
elif data_type == 'hash':
keys = redis_from.hkeys(k)
for key in keys:
value = redis_from.hget(k, key)
redis_to.hset(k, key, value)
else:
print 'not known type'
cnt = cnt + 1
print 'total', cnt
边栏推荐
- 电脑绘画软件哪个好用:试试Artweaver Plus吧,媲美sai绘画软件 | 最新版本的artweaver下载
- Matlab绘制激活函数sigmoid,Relu
- 企业钟情于混合 App 开发,小程序容器技术能让效率提升 100%
- 数学建模比赛论文模板格式
- What happens when MySQL tables change from compressed tables to ordinary tables?
- Understanding of random forest
- 剑指 Offer 60. n个骰子的点数
- The software supply chain security risk that makes enterprise digitalization and it executives bear the brunt of the cauldron points to the north
- 一种鲁棒变形卷积神经网络图像去噪
- 时间轴组件
猜你喜欢
Burpsuite2022.1 detailed installation steps include certificate installation
缩短饿了么tabs 组件线条宽度
No, check it out
(21)Blender源码分析之鼠标按下消息添加到队列的过程
Assembly line technology
TS的使用案例——贪吃蛇
【无标题】
[nodejs] npm/nrm cannot load the file because the script solution is prohibited in this system
Penetration test-01 information collection
Redis数据迁移:方法二AOF
随机推荐
【无标题】
[nodejs] npm/nrm cannot load the file because the script solution is prohibited in this system
AcWing:第60场周赛
自然语言处理 知识点积累
2022 electrician Cup: emergency material distribution in 5g network environment (optimization)
渗透测试-01信息收集
Chapter 4 user data analysis
v-for 中 key 的作用
时间轴组件
Vision Transformer(1):Self-attention Multi-head Self-attention
渗透测试-02漏洞扫描
Derivation of PCA principal component analysis (dimension reduction) process
Unity using Sqlite
Chapter 4 用户数据分析
windows10:vscode下go语言的适配
Frrouting use
2022长三角数学建模:齿轮箱故障诊断
Properties of Gaussian distribution (including code)
Enterprises love hybrid app development, and applet container technology can improve efficiency by 100%
Shorten the line width of tabs component