Develop Python Apps

Installation

Install the Python driver using the following command.

  1. $ sudo pip install yedis

Working example

Prerequisites

This tutorial assumes that you have:

  • installed YugabyteDB, created a universe, and are able to interact with it using the Redis shell. If not, please follow these steps in the quick start guide.

Writing the Python code

Create a file yb-redis-helloworld.py and add the following content to it.

  1. import redis
  2. # Create the cluster connection.
  3. r = redis.Redis(host='localhost', port=6379)
  4. # Insert the user profile.
  5. userid = 1
  6. user_profile = {"name": "John", "age": "35", "language": "Python"}
  7. r.hmset(userid, user_profile)
  8. print "Inserted userid=1, profile=%s" % user_profile
  9. # Query the user profile.
  10. print r.hgetall(userid)

Running the application

To run the application, type the following:

  1. $ python yb-redis-helloworld.py

You should see the following output.

  1. Inserted userid=1, profile={'age': '35', 'name': 'John', 'language': 'Python'}
  2. {'age': '35', 'name': 'John', 'language': 'Python'}