Study notes from DS201: Foundations of Apache Cassandra™ and DataStax Enterprise.
To connect to a Cassandra database from an application and manipulate data, you use a driver corresponding to your programming language. This guide explains how to use the Python driver to connect to Apache Cassandra and read data.
Installing the Cassandra Python Driver
First, install the Cassandra Python Driver:
pip install cassandra-driver
Python Code Example
The following Python script connects to a Cassandra cluster and reads data from the videos_by_tag table:
from cassandra.cluster import Cluster
import datetime # For handling datetime objects
# Establish connection to the Cassandra cluster
# By default, it tries to connect to localhost:9042
# protocol_version may need to be adjusted depending on the Cassandra version
cluster = Cluster(protocol_version=3)
# Establish a session
# 'killrvideo' is the target keyspace name
session = cluster.connect('killrvideo')
# Execute a CQL query
# SELECT * FROM videos_by_tag is a full table scan without specifying a partition key,
# which can cause performance issues on large tables and is not recommended in production.
# This example is for learning purposes.
rows = session.execute("SELECT * FROM videos_by_tag")
# Display the retrieved data
for row in rows:
print(row)
# Close the connection
cluster.shutdown()
Running the Code
Save the above Python script as test.py and run it:
python test.py
Example Output
Row(tag='datastax', added_date=datetime.datetime(2013, 10, 16, 9, 0), video_id=UUID('4845ed97-14bd-11e5-8a40-8338255b7e33'), title='DataStax Studio')
Row(tag='datastax', added_date=datetime.datetime(2013, 4, 16, 9, 0), video_id=UUID('5645f8bd-14bd-11e5-af1a-8638355b8e3a'), title='What is DataStax Enterprise?')
Row(tag='cassandra', added_date=datetime.datetime(2014, 1, 29, 9, 0), video_id=UUID('1645ea59-14bd-11e5-a993-8138354b7e31'), title='Cassandra History')
Row(tag='cassandra', added_date=datetime.datetime(2013, 3, 17, 9, 0), video_id=UUID('3452f7de-14bd-11e5-855e-8738355b7e3a'), title='Cassandra Intro')
Row(tag='cassandra', added_date=datetime.datetime(2012, 4, 3, 9, 0), video_id=UUID('245e8024-14bd-11e5-9743-8238356b7e32'), title='Cassandra & SSDs')
By using the Python driver, you can connect to a Cassandra database from your application, execute CQL queries, and retrieve or manipulate data.