Python – How do I use Python to execute get commands in Redis cluster mode?

How do I use Python to execute get commands in Redis cluster mode?… here is a solution to the problem.

How do I use Python to execute get commands in Redis cluster mode?

I want to use Python to perform the following scenario on Redis.

Using the command line:
1)redis-cli -c
2) redis_prompt >> Get some_string

I tried using the redis and rediscluster modules, but it didn’t work. Here’s the code I’ve tried:

1)

r = redis. Redis(host='123.123.123.123', port=6379, db=0)
r.get('some srting')

The following error occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\redis\client.py", line 880, in get
    return self.execute_command('GET', name)
  File "C:\Python27\lib\site-packages\redis\client.py", line 573, in execute_command
    return self.parse_response(connection, command_name, **options)
  File "C:\Python27\lib\site-packages\redis\client.py", line 585, in parse_response
    response = connection.read_response()
  File "C:\Python27\lib\site-packages\redis\connection.py", line 582, in read_response
    raise response
redis.exceptions.ResponseError: MOVED 9442 172.16.176.36:6380

2)

from rediscluster import StrictRedisCluster
startup_nodes = [{"host": "123.123.123.123", "port": "7000"}]
rc_readonly = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True, readonly_mode=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\rediscluster\client.py", line 181, in __init__
    **kwargs
  File "C:\Python27\lib\site-packages\rediscluster\connection.py", line 353, in __init__
    **connection_kwargs)
  File "C:\Python27\lib\site-packages\rediscluster\connection.py", line 141, in __init__
    self.nodes.initialize()
  File "C:\Python27\lib\site-packages\rediscluster\nodemanager.py", line 240, in initialize
    raise RedisClusterException("Redis Cluster cannot be connected. Please provide at least one reachable node.")
rediscluster.exceptions.RedisClusterException: Redis Cluster cannot be connected. Please provide at least one reachable node.

Any help would be great.

Solution

Ok, so this error means one of the following:

  • In these lines of code (1):

    r = redis. Redis(host='123.123.123.123', port=6379, db=0)
    
    r.get('some srting')
    

    You try to get the key “some sting” from host 123.123.123.123 and
    Port 6379. Error redis.exceptions.ResponseError: MOVED 9442
    172.16.176.36:6380
    indicates that this key is located at 172.16.176.36:6380. Therefore, try to use the . /redis-cli -c -p 6380 connects, and then
    EXECUTE CLUSTER GETKEYSINSLOT 6380 3 TO SEE IF THIS KEY REALLY IS
    Over here.

  • From (2) This error: rediscluster.exceptions.RedisClusterException: Unable to connect to Redis cluster. Please provide at least an reachable node indicates that there is a problem with the cluster. Perhaps, you didn’t initialize it properly.

    First in /home/{{user}}/redis-yourversion/utils/create-cluster you
    The create-cluster.sh will be found. Set the ports and hosts and nodes, and then
    Execute ./create-cluster start ./create-cluster-create. But first you
    The Redis server must be started in cluster mode. Go to redis.conf and in You will see the configuration parameters for cluster-enabled no. Set up
    Yes. After this you must run ./redis-server: /redis.conf (give
    path to redis.conf). If the cluster is enabled correctly, you will see Running in cluster mode

Important things to remember:

The parameter -c in the terminal indicates that the redis node can perform a redirect and find the correct node to which the key belongs without a MOVED error. If you have a driver assuming manipulating nodes in Python and getting keys or whatever you have to manage yourself for these redirects.

Related Problems and Solutions