Skip to content

Instantly share code, notes, and snippets.

@forkfork
Created May 7, 2022 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forkfork/98ade006ac3142fea8dd47016ad02ac7 to your computer and use it in GitHub Desktop.
Save forkfork/98ade006ac3142fea8dd47016ad02ac7 to your computer and use it in GitHub Desktop.
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