Scylla Driver for Apache Cassandra
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
- breaking Python 3.8 and 3.9 are no longer officially supported by the driver as of recent versions (e.g., 3.29.5 dropped Python 3.9, 3.29.4 dropped Python 3.8). While PyPI metadata indicates `>=3.9`, the official documentation states support for Python 3.10-3.14.
- breaking Support for Cassandra native protocol versions 1 and 2 has been removed. Connections to very old ScyllaDB or Apache Cassandra clusters that only support these protocols will fail.
- gotcha The driver's build system has moved from `setup_requires` to PEP 517. While this improves modern Python packaging, it might affect users with custom or older build environments if they were relying on `setup_requires` behavior.
- gotcha The default event loop in Python versions prior to 3.12 utilized `asyncore`. With `asyncore` being removed entirely in Python 3.12, users must explicitly configure an alternative event loop like `libev`, `gevent`, or `eventlet` when running on Python 3.12+ if they relied on the default.
- deprecated The `Session.default_consistency_level` attribute is deprecated. Relying on this attribute may lead to future compatibility issues.
Install
-
pip install scylla-driver
Imports
- Cluster
from cassandra.cluster import Cluster
- Session
from cassandra.cluster import Cluster; session = cluster.connect()
- PlainTextAuthProvider
from cassandra.auth import PlainTextAuthProvider
Quickstart
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()