#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: lg
@contact: [email protected]
@software: PyCharm
@file: transfer_redis.py
@create at: 2018-09-12
这一行开始写关于本文件的说明与解释
"""
from crwy.utils.logger import Logger
from crwy.utils.no_sql.redis_m import get_redis_client
logger = Logger.file_logger()
org_db = get_redis_client(
host='127.0.0.1',
port='6379',
password='',
db=0,
cls_singleton=False
)
trg_db = get_redis_client(
host='127.0.0.1',
port='6379',
password='',
db=2,
cls_singleton=False
)
def main():
# 游标
cursor = 0
while True:
try:
cursor, keys = org_db.scan(cursor=cursor, count=10000)
if len(keys) == 0:
logger.info('finish')
break
for key in keys:
hs1 = org_db.hgetall(key)
for hs2 in hs1:
phone = org_db.hget(key, hs2)
res_lst = hs2.split("|")
if len(res_lst) != 2:
continue
a, b = res_lst
if not phone:
logger.info('skip when phone is empty: {} {}'.format(key, hs2))
continue
logger.info('origin_key: {}, {} {} {}'.format(key, a, b, phone))
b1 = list(set(b.split("#")))
for b3 in b1:
if len(b3) > 20:
continue
new_key = key + b3
with trg_db.pipeline() as r:
r.hset(new_key, a, phone)
r.execute()
except Exception as e:
logger.exception(e)
if __name__ == '__main__':
main()