Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using Python with Cassandra

Introduction

Cassandra is a highly scalable NoSQL database designed for high availability and performance. Python is a popular programming language that allows for easy interaction with databases like Cassandra. This tutorial will guide you through the process of using Python with Cassandra from installation to executing queries.

Prerequisites

Before you begin, ensure you have the following:

  • Python 3.x installed on your machine.
  • Cassandra installed and running.
  • Basic knowledge of Python programming.

Installing Required Libraries

To interact with Cassandra using Python, you need to install the cassandra-driver library. You can install it using pip:

pip install cassandra-driver

After successful installation, you can start using the driver in your Python scripts.

Connecting to Cassandra

To connect to your Cassandra database, you will need to import the necessary libraries and establish a connection. Here's an example:

from cassandra.cluster import Cluster

# Connect to the Cassandra cluster
cluster = Cluster(['127.0.0.1'])
session = cluster.connect()
                

In the above code, replace ['127.0.0.1'] with your Cassandra server IP address if it's running on a different machine.

Creating a Keyspace

A keyspace in Cassandra is like a database schema. It defines how data is replicated across the nodes. To create a keyspace, you can use the following code:

session.execute("""
    CREATE KEYSPACE IF NOT EXISTS my_keyspace
    WITH REPLICATION = { 'class': 'SimpleStrategy', 'replication_factor': 1 }
""")
                

This example creates a keyspace named my_keyspace with a replication factor of 1.

Creating a Table

Once you have a keyspace, you can create tables to store your data. Here’s how to create a simple table:

session.set_keyspace('my_keyspace')

session.execute("""
    CREATE TABLE IF NOT EXISTS users (
        user_id UUID PRIMARY KEY,
        username text,
        email text
    )
""")
                

This creates a table named users with three columns: user_id, username, and email.

Inserting Data

To insert data into the table you created, you can use the following syntax:

from uuid import uuid4

session.execute("""
    INSERT INTO users (user_id, username, email)
    VALUES (%s, %s, %s)
""", (uuid4(), 'john_doe', 'john@example.com'))
                

This snippet inserts a new user into the users table.

Querying Data

To retrieve data from the table, use the SELECT statement. Here’s how you can fetch all users:

rows = session.execute("SELECT * FROM users")
for row in rows:
    print(row.username, row.email)
                

This will print the username and email of all users stored in the table.

Closing the Connection

Once you are done with your operations, it's a good practice to close the session and connection:

session.shutdown()
cluster.shutdown()
                

Conclusion

In this tutorial, you learned how to use Python with Cassandra, from installation to performing basic operations like creating a keyspace, table, inserting data, and querying data. With this knowledge, you can now build applications that leverage the power of Cassandra using Python.