How to connect to Apache Cassandra with Python?
This is going to be a short tutorial for the beginners. In this tutorial, we will learn how to connect to an Apache Cassandra database using Python, create a table, and perform some basic queries.
We will use the cassandra-driver
package to interact with the Cassandra database.
Prerequisites:
- Python 3.6 or higher
- An Apache Cassandra database instance running and accessible
- And also, cassandra-driver Python package installed (you can install it using “pip install cassandra-driver”)
After everything is set, let’s get started.
Step 1: Importing the necessary modules
First, let’s import the required modules from the cassandra-driver
package:
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
Step 2: Connecting to the Cassandra database
Next, we will establish a connection to the Cassandra database. Replace username
, password
, and ip_address
with your own credentials and database information:
auth_provider = PlainTextAuthProvider(username='your_username', password='your_password')
cluster = Cluster(contact_points=['your_ip_address'], auth_provider=auth_provider)
session = cluster.connect()
Step 3: Creating a keyspace
Before creating tables, let’s create a keyspace (similar to a schema in relational databases) for our tables:
keyspace = "my_keyspace"
session.execute(f"CREATE KEYSPACE IF NOT EXISTS {keyspace} WITH replication = {{'class': 'SimpleStrategy', 'replication_factor': 3}};")
session.set_keyspace(keyspace)
Step 4: Creating a table
Now, let’s create a simple table called users
to store user information:
create_table_query = """
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
first_name text,
last_name text,
age int
);
"""
session.execute(create_table_query)
Step 5: Inserting data into the table
To insert data into the users
table, we will use the INSERT
statement:
from uuid import uuid4
insert_query = "INSERT INTO users (id, first_name, last_name, age) VALUES (%s, %s, %s, %s)"
user_data = (uuid4(), "John", "Doe", 30)
session.execute(insert_query, user_data)
Step 6: Querying data from the table
To retrieve data from the users
table, we will use the SELECT
statement:
select_query = "SELECT * FROM users"
rows = session.execute(select_query)
for row in rows:
print(f"{row.id}: {row.first_name} {row.last_name} ({row.age} years old)")
Step 7: Updating data in the table
In order to update data in the users
table, we will use the UPDATE
statement. First, copy the id
of the user you want to update from the previous step, then replace new_age
with the updated value:
update_query = "UPDATE users SET age = %s WHERE id = %s"
updated_data = (new_age, user_id)
session.execute(update_query, updated_data)
Step 8: Deleting data from the table
We will use the DELETE
statement to delete data from the users
table. Replace user_id
with the id
of the user you want to delete:
delete_query = "DELETE FROM users WHERE id = %s"
session.execute(delete_query, (user_id,))
Step 9: Closing the connection
After you finish working with the Cassandra database, don’t forget to close the connection to the cluster. This will free up resources and prevent any potential connection leaks:
cluster.shutdown()
Congratulations! You have successfully connected to an Apache Cassandra database using Python, created a keyspace and a table, and performed basic CRUD operations. You can now apply these concepts to interact with your own Cassandra databases and build more complex applications using Python.
Thank you for your valuable time!
See you next time people.