Got it

Connection Between Python and the Redis Cluster

Latest reply: Nov 21, 2021 15:42:43 381 4 3 0 0

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!

  • x
  • convention:

little_fish
Admin Created Apr 7, 2021 09:44:25

good
View more
  • x
  • convention:

Unicef
MVE Created Apr 7, 2021 13:43:41

Well done
View more
  • x
  • convention:

olive.zhao
Admin Created Nov 15, 2021 02:32:30

Thanks for your sharing!
View more
  • x
  • convention:

user_4358465
Created Nov 21, 2021 15:42:43

Thanks for sharing this useful info
View more
  • x
  • convention:

Comment

You need to log in to comment to the post Login | Register
Comment

Notice: To protect the legitimate rights and interests of you, the community, and third parties, do not release content that may bring legal risks to all parties, including but are not limited to the following:
  • Politically sensitive content
  • Content concerning pornography, gambling, and drug abuse
  • Content that may disclose or infringe upon others ' commercial secrets, intellectual properties, including trade marks, copyrights, and patents, and personal privacy
Do not share your account and password with others. All operations performed using your account will be regarded as your own actions and all consequences arising therefrom will be borne by you. For details, see " User Agreement."

My Followers

Login and enjoy all the member benefits

Login

Block
Are you sure to block this user?
Users on your blacklist cannot comment on your post,cannot mention you, cannot send you private messages.
Reminder
Please bind your phone number to obtain invitation bonus.
Information Protection Guide
Thanks for using Huawei Enterprise Support Community! We will help you learn how we collect, use, store and share your personal information and the rights you have in accordance with Privacy Policy and User Agreement.