Skip to content

Instantly share code, notes, and snippets.

@marcieltorres
Created January 24, 2024 15:20
Show Gist options
  • Save marcieltorres/456a9111f67adf9251793d7ebf46e047 to your computer and use it in GitHub Desktop.
Save marcieltorres/456a9111f67adf9251793d7ebf46e047 to your computer and use it in GitHub Desktop.
Get keys from redis using zrange and generate output csv file
import json
from timeit import default_timer as timer
import csv
from redis import ConnectionPool, Redis
__REDIS_HOST = ''
__PAGE_SIZE = 500
__OUTPUT_FILE_NAME = ''
__ZKEY_NAME = ''
def create_cache_connection():
connection_pool = ConnectionPool(
host=__REDIS_HOST,
port=6379,
)
return Redis(connection_pool=connection_pool)
def create_csv(output_file, rows):
with open(output_file, 'a') as file:
writer = csv.writer(file, delimiter='\n')
writer.writerow(rows)
def time_in_minutes(seconds):
return "%.2f" % (seconds / 60)
def main():
print("Starting job")
start_time_job = timer()
total_keys = 0
cache = create_cache_connection()
page = 0
should_continue = True
while should_continue:
start = page * __PAGE_SIZE
stop = start + __PAGE_SIZE - 1
keys = cache.zrange(__ZKEY_NAME, start, stop)
print(f"\nGetting {len(keys)} keys from page {page}")
if len(keys):
keys_id = []
for item in keys:
id = json.loads(item).get('key_name')
keys_id.append(id)
total_keys = total_keys + len(keys)
print(f"Saving {len(keys_id)} keys on csv file")
create_csv(__OUTPUT_FILE_NAME, keys_id)
page = page + 1
else:
should_continue = False
cache.close()
time_elapsed = time_in_minutes(timer() - start_time_job)
print(f"\n{total_keys} keys processed in {time_elapsed} minutes\n")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment