Created
May 7, 2022 13:13
-
-
Save forkfork/98ade006ac3142fea8dd47016ad02ac7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import csv | |
import redis | |
import sys | |
from itertools import zip_longest | |
r = redis.Redis(host='localhost', port=6379) | |
csvwriter = csv.writer(sys.stdout) | |
def batcher(iterable, n): | |
args = [iter(iterable)] * n | |
return zip_longest(*args) | |
for keybatch in batcher(r.scan_iter('*'),2000): | |
pipe = r.pipeline() | |
for key in keybatch: | |
if key is not None: | |
pipe.type(key) | |
types = pipe.execute() | |
batchgets = [] | |
for idx, eachtype in enumerate(types): | |
if eachtype == b"string": | |
batchgets.append(keybatch[idx]) | |
pipe = r.pipeline() | |
for eachget in batchgets: | |
pipe.get(eachget) | |
batchget_results = pipe.execute() | |
for idx, eachresult in enumerate(batchget_results): | |
csvwriter.writerow([batchgets[idx].decode("utf8"), eachresult.decode("utf8")]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment