It's a continuation of this I threw lua to redis-server and scanned it in the server to return the key and value, so I made a note of it.
$ python -V
Python 3.7.2
$ redis-cli info | grep redis_version
redis_version:5.0.5
When using hash or set, read as HSCAN
or ZSCAN
as appropriate.
dump.lua
local cr = KEYS[1]
local num = KEYS[2]
local match = KEYS[3]
local cr2, keys = unpack(redis.call("SCAN", cr, "MATCH", match, "COUNT", num))
local values = redis.pcall("MGET", unpack(keys))
return {cr2, keys, values}
export.py
#!/usr/bin/env python
import os
import redis
import click
DEFAULT_REDIS_URL = "redis://localhost:6379/3"
DEFAULT_LUA_FILE = "dump.lua"
def scan_iter(r, lua_script, bulk=2000, match="*"):
f = r.register_script(lua_script)
cr = "0"
while True:
cr, keys, values = f(keys=[cr, bulk, match])
if cr == b"0":
break
yield from zip(keys, values)
@click.command()
@click.option("--redis-url", envvar="REDIS_URL", default=DEFAULT_REDIS_URL)
@click.option("--lua-file", envvar="LUA_FILE", default=DEFAULT_LUA_FILE, type=click.File("r"))
@click.option("--output", default=os.sys.stdout, type=click.File("w"))
def cmd(redis_url, lua_file, output):
r = redis.StrictRedis.from_url(redis_url)
r.ping()
for k, v in scan_iter(r, lua_file.read()):
output.write(k.decode("utf-8"))
output.write("\n")
output.write("%d" % len(v))
output.write("\n")
if __name__ == "__main__":
cmd()
$ pip install redis click
$ jq -n -r 'range(10000)|"set key\(.) value\(.)"' | redis-cli -n 8
$ ./export.py --redis-url redis://localhost:6379/8
end.
Recommended Posts