前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >利用python 迁移单个redis的

利用python 迁移单个redis的

作者头像
py3study
发布2020-01-02 14:54:11
5660
发布2020-01-02 14:54:11
举报
文章被收录于专栏:python3python3python3

环境的是python3 / pip3

import sys

import redis


# 迁移hash
def moveHash(cursor):
    cursor, data = r.hscan(key, cursor)
    for eachKey in data:
        rNew.hset(key, eachKey, data[eachKey])
    print(key, "---处理了---", len(data), '个')
    if cursor != 0:
        print(cursor, "批处理")
        moveHash(cursor)
    else:
        print(cursor, "处理完成了")


# 迁移list
def moveList():
    length = r.llen(key)
    if length == 0:
        print(key, "---list迁移结束---剩余长度", length)
    else:
        # 每次迁移一千个
        start = length - 1000;
        if start < 0:
            start = 0
        data = r.lrange(key, start, -1)
        pl = r.pipeline();
        for eachI in data:
            setAdd = r.sadd("ordernokey_move", eachI);
            if setAdd == 1:
                pl.rpush("aaaaaaa", eachI)
            else:
                print("迁移的key的值重复了", eachI)
        pl.execute()
        if start == 0:
            # 清空
            r.ltrim(key, 1, 0)
        r.ltrim(key, 0, start - 1)
        moveList()


############################


key = sys.argv[1]

print('输入的key是:' + key)
# ip = '47.254.149.109'
# password = 'Kikuu2018'

ip1 = '115.236.170.78'
password1 = 'kikuu2018'

ip2 = '115.236.170.78'
password2 = 'kikuu2018'

# 连接redis
r = redis.Redis(host=ip1, password=password1, port=6379, db=0,
                decode_responses=True)

# 连接redis  带接收的库
rNew = redis.Redis(host=ip2, password=password2, port=6379, db=0,
                   decode_responses=True)

keyType = r.type(key)

if keyType == 'string':
    rNew.set(key, r.get(key))
    print("key=" + key + "迁移到新库")

if keyType == 'hash':
    cursor = r.hlen(key)
    print(" key值长度是 + ", cursor)
    moveHash(0)

if keyType == 'list':
    moveList()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档