本文主要是介绍redis-cli通过DUMP和SADD进行redis set数据类型迁移,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、DUMP指定key导出
redis-cli -h redis1.aliyuncs.com -n 1 -a abc DUMP SOL_addresses
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"\x02A#,7tC9xSy8FZ9idnDb3e8DaXH6mQYLDWzaDovvQmYESjCQ,DCbNevBcwToUArdEDx3Q7kcLhjezcYuoNfZ2a6HTKYLS,6jA5SWhr5UkLjAKxpuo3ph8jkbRF5w2okZeV5s49ACpU\t\x00\xc8\x90k\xd1\x03Z$w"
2、导入
1)编写python脚本
import redis# 连接到Redis
r = redis.StrictRedis(host='redis2.aliyuncs.com', port=6379, password='123', db=10)# 将序列化数据转换为字符串
serialized_data = "\x02A#,7tC9xSy8FZ9idnDb3e8DaXH6mQYLDWzaDovvQmYESjCQ,DCbNevBcwToUArdEDx3Q7kcLhjezcYuoNfZ2a6HTKYLS,6jA5SWhr5UkLjAKxpuo3ph8jkbRF5w2okZeV5s49ACpU\t\x00\xc8\x90k\xd1\x03Z$w"# 解析字符串并添加到 set 中
data_list = serialized_data.split(',') # 假设数据以逗号分隔
for item in data_list:r.sadd("SOL_addresses", item)
2)安装redis模块
pip3 install redis
Looking in indexes: http://mirrors.cloud.aliyuncs.com/pypi/simple/
Collecting redisDownloading http://mirrors.cloud.aliyuncs.com/pypi/packages/bb/f1/a384c5582d9a28e4a02eb1a2c279668053dd09aafeb08d2bd4dd323fc466/redis-5.0.3-py3-none-any.whl (251 kB)|████████████████████████████████| 251 kB 1.3 MB/s
Collecting async-timeout>=4.0.3; python_full_version < "3.11.3"Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/a7/fa/e01228c2938de91d47b307831c62ab9e4001e747789d0b05baf779a6488c/async_timeout-4.0.3-py3-none-any.whl (5.7 kB)
Installing collected packages: async-timeout, redis
Successfully installed async-timeout-4.0.3 redis-5.0.3
3、执行脚本导入数据
python3 python-set.py
这篇关于redis-cli通过DUMP和SADD进行redis set数据类型迁移的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!