Develop Python Apps
AttentionThis page documents an earlier version. Go to the latest (v2.1)version.
Installation
Install the python driver using the following command.
$ pip install cassandra-driver
Working Example
Pre-requisites
This tutorial assumes that you have:
- installed YugabyteDB, created a universe and are able to interact with it using the CQL shell. If not, please follow these steps in the quick start guide.
Writing the python code
Create a file yb-cql-helloworld.py
and add the following content to it.
from cassandra.cluster import Cluster
# Create the cluster connection.
cluster = Cluster(['127.0.0.1'])
session = cluster.connect()
# Create the keyspace.
session.execute('CREATE KEYSPACE IF NOT EXISTS ybdemo;')
print "Created keyspace ybdemo"
# Create the table.
session.execute(
"""
CREATE TABLE IF NOT EXISTS ybdemo.employee (id int PRIMARY KEY,
name varchar,
age int,
language varchar);
""")
print "Created table employee"
# Insert a row.
session.execute(
"""
INSERT INTO ybdemo.employee (id, name, age, language)
VALUES (1, 'John', 35, 'NodeJS');
""")
print "Inserted (id, name, age, language) = (1, 'John', 35, 'Python')"
# Query the row.
rows = session.execute('SELECT name, age, language FROM ybdemo.employee WHERE id = 1;')
for row in rows:
print row.name, row.age, row.language
# Close the connection.
cluster.shutdown()
Running the application
To run the application, type the following:
$ python yb-cql-helloworld.py
You should see the following output.
Created keyspace ybdemo
Created table employee
Inserted (id, name, age, language) = (1, 'John', 35, 'Python')
John 35 Python
Installation
Install the python driver using the following command.
$ sudo pip install redis
Working Example
Pre-requisites
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.
import redis
# Create the cluster connection.
r = redis.Redis(host='localhost', port=6379)
# Insert the user profile.
userid = 1
user_profile = {"name": "John", "age": "35", "language": "Python"}
r.hmset(userid, user_profile)
print "Inserted userid=1, profile=%s" % user_profile
# Query the user profile.
print r.hgetall(userid)
Running the application
To run the application, type the following:
$ python yb-redis-helloworld.py
You should see the following output.
Inserted userid=1, profile={'age': '35', 'name': 'John', 'language': 'Python'}
{'age': '35', 'name': 'John', 'language': 'Python'}
当前内容版权归 YugabyteDB 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 YugabyteDB .