Introduction to Cassandra Driver
The Cassandra Driver is a powerful tool that enables developers to interact with Apache Cassandra databases. It offers a variety of APIs to perform database operations effectively and efficiently. In this guide, we will explore some of the essential APIs provided by the Cassandra Driver, complete with code snippets and a comprehensive application example to help you understand their usage.
Connecting to Cassandra
from cassandra.cluster import Cluster cluster = Cluster(['127.0.0.1']) session = cluster.connect()
This snippet establishes a connection to a Cassandra cluster running on the local machine.
Creating a Keyspace
session.execute(""" CREATE KEYSPACE IF NOT EXISTS mykeyspace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 } """)
This code creates a keyspace named mykeyspace
with a replication factor of 3.
Creating a Table
session.execute(""" CREATE TABLE mykeyspace.users ( user_id uuid PRIMARY KEY, username text, email text ) """)
This snippet creates a table named users
in the mykeyspace
keyspace.
Inserting Data
from uuid import uuid4 session.execute(""" INSERT INTO mykeyspace.users (user_id, username, email) VALUES (%s, %s, %s) """, (uuid4(), 'alice', 'alice@example.com'))
Inserts a new user record into the users
table.
Querying Data
rows = session.execute("SELECT * FROM mykeyspace.users") for row in rows: print(row.username, row.email)
This snippet retrieves and prints all records from the users
table.
Updating Data
session.execute(""" UPDATE mykeyspace.users SET email = %s WHERE user_id = %s """, ('alice_new@example.com', user_id))
This snippet updates the email address for a specific user.
Deleting Data
session.execute(""" DELETE FROM mykeyspace.users WHERE user_id = %s """, (user_id,))
This snippet deletes a user record from the users
table.
Full Application Example
from cassandra.cluster import Cluster from uuid import uuid4 # Connect to the cluster cluster = Cluster(['127.0.0.1']) session = cluster.connect() # Create keyspace and table session.execute(""" CREATE KEYSPACE IF NOT EXISTS mykeyspace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 } """) session.execute(""" CREATE TABLE IF NOT EXISTS mykeyspace.users ( user_id uuid PRIMARY KEY, username text, email text ) """) # Insert data session.execute(""" INSERT INTO mykeyspace.users (user_id, username, email) VALUES (%s, %s, %s) """, (uuid4(), 'alice', 'alice@example.com')) # Query data rows = session.execute("SELECT * FROM mykeyspace.users") for row in rows: print(row.username, row.email) # Update data session.execute(""" UPDATE mykeyspace.users SET email = %s WHERE user_id = %s """, ('alice_new@example.com', user_id)) # Delete data session.execute(""" DELETE FROM mykeyspace.users WHERE user_id = %s """, (user_id,))
This full application example demonstrates how to connect to a Cassandra cluster, create a keyspace and table, insert, query, update, and delete data using the Cassandra Driver.
By understanding and leveraging these APIs, you can effectively manage your Cassandra databases and build robust applications that interact with Cassandra seamlessly.
Hash: 3c13ef10c3b2753d32d8b5f6bb8d6da3d043e3850a05eb351527f5f329735759