Reputation: 61
I am trying to get multiple values from Redis using Python .
mget - all keys must map to the same key slot
Below is the code snippet (Python Library)
import redis
rc = redis.RedisCluster(host,port)
all_keys_list = rc.get_client().keys("UniqueID*")
all_values = rc.get_client().mget(all_keys_list )
Error: mget - all keys must map to the same key slot
Upvotes: 4
Views: 2291
Reputation: 824
There are two ways to accomplish getting multiple keys mapped to different slots
You can use curly braces to ensure they all end up on the same slot {UniqueID}10 and {UniqueID}11 will be on the same slot since only the name inside the braces is hashed.
Instead of using MGET use a pipeline being sure to set transaction to False in python
pipe = client.pipeline(transaction=False)
if len(sys.argv) > 1:
load_file = sys.argv[1]
else
load_file = 'pop_users.csv'
with open(load_file, newline='') as csvfile:
reader = csv.DictReader(csvfile)
row_count = 0
for row in reader:
pipe.hset("user:%s" %(row['username']), mapping = row)
row_count += 1
if row_count % 500 == 0:
pipe.execute()
pipe.execute()
Upvotes: 3