Python redis导出与导入

导出:

r = redis.Redis(host = RDSHOSTADDR,password= RDSPWDADDR , db=RDSDBADDR)
h = open('redis.txt', 'w')
def redis_scan():
    cursor = 1
    isNoe = True
    while cursor != 0:
        if isNoe:
            cursor = 0
            isNoe = False
        key = r.scan(cursor, count=2000)        #每次拿2000个key
        time.sleep(0.05)
        if len(key[1]) == 0:
            print("key scan finish")
        else:
            print len(key[1])
            lis, res = [], []
            for n in key[1]:
                n = bytes.decode(n)
                lis.append(n)
            cursor = key[0]
            value = r.mget(lis)
            for i in range(len(value)):
                dic = {lis[i]:value[i]}
                res.append(dic)
            h.write(json.dumps(res)+'\n')
h.flush()
redis_scan()

导入:

import redis
import json

r = redis.Redis(host=)

h = open('redis.txt', 'r')

line = h.readline()

while line:
    value = json.loads(line)
    dic = {}
    for i in value:
        dic.update(i)
    r.mset(dic)
    line = h.readline()

print 'finish'
~                       

猜你喜欢

转载自blog.csdn.net/qq_40771567/article/details/105082113