Scylla Driver for Apache Cassandra

3.29.9 · active · verified Mon Apr 13

The Scylla Python Driver is a modern, feature-rich, and highly-tunable Python client library designed for Scylla Open Source (2.1+), Apache Cassandra (2.1+), and Scylla Enterprise (2018.1.x+). It exclusively uses Cassandra's binary protocol and Cassandra Query Language v3. The driver, currently at version 3.29.9, offers synchronous and asynchronous APIs, connection pooling, automatic node discovery, and includes an integrated object mapper (cqlengine). It maintains an active release cadence with frequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a connection to a ScyllaDB or Cassandra cluster, create a keyspace and table, insert data, and retrieve it. It uses environment variables for credentials, suitable for secure deployment.

import os
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider

# Replace with your ScyllaDB/Cassandra contact points and credentials
CONTACT_POINTS = os.environ.get('SCYLLA_CONTACT_POINTS', '127.0.0.1').split(',')
USERNAME = os.environ.get('SCYLLA_USERNAME', 'scylla')
PASSWORD = os.environ.get('SCYLLA_PASSWORD', 'password')

auth_provider = PlainTextAuthProvider(username=USERNAME, password=PASSWORD)

cluster = None
session = None
try:
    cluster = Cluster(contact_points=CONTACT_POINTS, auth_provider=auth_provider)
    session = cluster.connect()

    # Example: Create a keyspace and table (if they don't exist)
    session.execute(
        """CREATE KEYSPACE IF NOT EXISTS mykeyspace WITH replication = {'class': 'NetworkTopologyStrategy', 'datacenter1': 1}"""
    )
    session.set_keyspace('mykeyspace')
    session.execute(
        """CREATE TABLE IF NOT EXISTS users (id UUID PRIMARY KEY, name text, email text)"""
    )

    # Example: Insert data
    user_id = session.execute("SELECT uuid() FROM system.local").one()[0]
    session.execute(
        """INSERT INTO users (id, name, email) VALUES (%s, %s, %s)""",
        (user_id, "Alice", "alice@example.com")
    )
    print(f"Inserted user: {user_id}")

    # Example: Select data
    rows = session.execute("SELECT id, name, email FROM users WHERE id = %s", (user_id,))
    for row in rows:
        print(f"Retrieved user: {row.id}, {row.name}, {row.email}")

finally:
    if session:
        session.shutdown()
    if cluster:
        cluster.shutdown()

view raw JSON →