Abhishek Gupta
Abhishek Gupta

Reputation: 61

Redis [ Exception : mget - all keys must map to the same key slot ]

I am trying to get multiple values from Redis using Python .

  1. Using method redisClient.keys("UniqueID*") method to get list of keys that match pattern such as "UniqueID10", "UniqueID11", "UniqueID13"
  2. while passing the list of keys to method redisClient.mget([list of keys]) , i am getting the error 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

  1. Can this be solved from python using any other method or concurrency.
  2. Do I have to use slot hashing while putting keys and is it possible that all same keys entry do not land in same slot due to memory constraint of the slot and I will get this issue again.

Upvotes: 4

Views: 2291

Answers (1)

namizaru
namizaru

Reputation: 824

There are two ways to accomplish getting multiple keys mapped to different slots

  1. 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.

  2. 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

Related Questions