Last active
August 29, 2020 16:05
-
-
Save hey-jude/1a30e873a7b7a13c1e32da2dd994edd5 to your computer and use it in GitHub Desktop.
split redis command data with key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from __future__ import print_function | |
import sys | |
from argparse import ArgumentParser | |
from rediscluster import RedisCluster | |
from contextlib import ExitStack | |
def eprint(*args, **kwargs): | |
print(*args, file=sys.stderr, **kwargs) | |
parser = ArgumentParser() | |
parser.add_argument('-host', help='redis hostname or ip', default='127.0.0.1') | |
parser.add_argument('-port', help='redis port number', default='6379') | |
parser.add_argument('-outputs', help='ouput file name template. NODE_NAME replaced by ip_port.', required=True) | |
args = parser.parse_args() | |
startup_nodes = [{"host": args.host, "port": args.port}] | |
rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True, readonly_mode=True) | |
cp = rc.connection_pool | |
master_nodes = list(map(lambda i: i[0], filter(lambda elem: elem[1]['server_type'] == 'master', cp.nodes.nodes.items()))) | |
eprint(master_nodes) | |
output_filenames = list(map(lambda s: args.outputs.replace('NODE_NAME', s.replace(':', '_')), master_nodes)) | |
eprint(output_filenames) | |
with ExitStack() as stack: | |
files = [stack.enter_context(open(fname, 'w')) for fname in output_filenames] | |
output_files = dict(zip(master_nodes, files)) | |
for line in sys.stdin: | |
key = line.split()[1].strip('"') | |
slot = cp.nodes.keyslot(key) | |
node = cp.get_master_node_by_slot(slot).get('name') | |
output_files[node].write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment