资讯专栏INFORMATION COLUMN

利用python 迁移单个redis的key。

Cciradih / 673人阅读

摘要:环境的是迁移处理了个批处理处理完成了迁移迁移结束剩余长度每次迁移一千个迁移的的值重复了清空输入的是连接连接带接收的库迁移到新库值长度是

环境的是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()

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/43822.html

相关文章

发表评论

0条评论

最新活动
阅读需要支付1元查看
<