Skip to content

Instantly share code, notes, and snippets.

@iknowjason
Last active November 14, 2021 11:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iknowjason/b5a11640107bca6f71624c4135e03389 to your computer and use it in GitHub Desktop.
Save iknowjason/b5a11640107bca6f71624c4135e03389 to your computer and use it in GitHub Desktop.
Redis sort unique key values
###
# Author: Jason Ostrom
###
#
# Description: Connect to a redis server and print unique values for keys
#
###
# 1. Install python redis client
# $sudo pip3 install redis
# 2. Edit your HOST
# 3. Run it:
# $python3 redis_unique_keys.py
###
import redis
HOST = "myhost.redis.local"
PORT = 6379
print("[+] Connecting to redis server %s:%s" % (HOST, PORT))
### Connect to redis server
client = redis.Redis(host=HOST, port=PORT, db=0)
print ("[+] Start loading redis keys")
data = {}
cursor = '0'
while cursor != 0:
cursor, keys = client.scan(cursor=cursor, count=1000000)
values = client.mget(*keys)
values = [value for value in values if not value == None]
data.update(dict(zip(keys, values)))
print ("[+] Finished Loading redis keys")
list1 = []
print ("[+] Load values")
for i in data:
list1.append(data[i])
my_set = set(list1)
my_new_list = list(my_set)
print("[+] Unique values for all keys:")
for i in my_new_list:
blah = i.decode("utf-8")
print(blah)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment