1
This commit is contained in:
parent
9c57b34a39
commit
99bedf1896
44
tools/migratedb/import_redis.py
Normal file
44
tools/migratedb/import_redis.py
Normal file
@ -0,0 +1,44 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#!/usr/bin/python
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import json
|
||||
import redis
|
||||
import datetime
|
||||
import pprint
|
||||
|
||||
argv = sys.argv
|
||||
host = argv[1]
|
||||
port = argv[2]
|
||||
script_name = argv[3]
|
||||
|
||||
def writeToRedis(conn, cmdline):
|
||||
assert len(cmdline) > 1
|
||||
# print(cmdline)
|
||||
if cmdline[0] == 'hset':
|
||||
conn.hset(cmdline[1], cmdline[2], cmdline[3])
|
||||
elif cmdline[0] == 'zadd':
|
||||
conn.zadd(cmdline[1], {cmdline[3]: cmdline[2]})
|
||||
elif cmdline[0] == 'sadd':
|
||||
conn.sadd(cmdline[1], cmdline[2])
|
||||
elif cmdline[0] == 'set':
|
||||
conn.set(cmdline[1], cmdline[2])
|
||||
elif cmdline[0] == 'expire':
|
||||
conn.expire(cmdline[1], cmdline[2])
|
||||
else:
|
||||
assert False
|
||||
|
||||
conn = redis.Redis(host = host,
|
||||
port = port,
|
||||
db = 0,
|
||||
decode_responses = True
|
||||
)
|
||||
|
||||
with open(script_name, 'r') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line == '':
|
||||
continue
|
||||
writeToRedis(conn, json.loads(line))
|
@ -41,18 +41,18 @@ def saveKeys(r, keys, curr_file):
|
||||
data = r.get(key)
|
||||
expire = r.ttl(key)
|
||||
curr_file.write(json.dumps(
|
||||
{
|
||||
'cmd': 'set',
|
||||
'key': key,
|
||||
'data':data
|
||||
}
|
||||
[
|
||||
'set',
|
||||
key,
|
||||
data
|
||||
]
|
||||
) + '\n')
|
||||
curr_file.write(json.dumps(
|
||||
{
|
||||
'cmd': 'expire',
|
||||
'key': key,
|
||||
'time': expire
|
||||
}
|
||||
[
|
||||
'expire',
|
||||
key,
|
||||
expire
|
||||
]
|
||||
) + '\n')
|
||||
if expire < 0:
|
||||
print('[ERROR] ' + key + ' ttl < 0')
|
||||
|
Loading…
x
Reference in New Issue
Block a user