Hi, everyone!
Today I'm going to introduce you to the connection between Python and the Redis cluster.
In this case, Python 3.6 is used.
Installation Dependency
Note: The dependency version must be the following:
1. Install Redis-py 2.10.6 first.
Download link:
https://github.com/andymccurdy/redis-py/archive/2.10.6.tar.gz
2. Install redis-py-cluster 1.3.5.
Download Link
https://files.pythonhosted.org/packages/f1/dd/4bb27bb3e3d03a01b0afd4a4ba13a4677b0f2d6552ff2841ac56591bfb29/redis-py-cluster-1.3.5.tar.gz
Download the file and upload it to the Linux environment.
After the decompression, go to the directory after the decompression.
Executing python3 setup.py install
Test Verification Scripts
The following code connects to the Redis in the FusionInsight cluster where the security mode is not enabled.
#!/usr/bin/env python3 from rediscluster import StrictRedisCluste class RedisCluster(object): def __init__(self, conn_list): self.conn_list = conn_list def connect(self): """ Connecting to the Redis Cluster :return: object """ try: redisconn = StrictRedisCluster(startup_nodes=self.conn_list) return redisconn except Exception as e: print(e) print("Error. Failed to connect to the Redis cluster.") return False redis_basis_conn = [{'host': '10.244.230.10', 'port': 22400}, {'host': '10.244.230.207', 'port': 22400}, {'host': '10.244.230.58', 'port': 22400}] res = RedisCluster(redis_basis_conn).connect() if not res: print("Failed to connect to the Redis cluster.") else: print("Connecting to the Redis cluster succeeded.") redis_conn = RedisCluster(redis_basis_conn).connect() redis_conn.set('name', 'admin') print("name is: ", redis_conn.get('name'))
Hope you can learn from it, thank you!