Interacting with ScyllaDB in Python

ScyllaDB is a high-performance distributed NoSQL database compatible with Apache Cassandra. Thanks to the cassandra-driver library from DataStax, it is easy to interact with ScyllaDB using Python. In this article, we'll see how to connect to a cluster, create a table, insert, and read data.

Installing the Cassandra Driver

To get started, you need to install the official driver:

pip install cassandra-driver

Connecting to the ScyllaDB Cluster

To connect to the cluster, simply specify the IPs of the nodes:

from cassandra.cluster import Cluster

# Connecting to the ScyllaDB cluster
cluster = Cluster(['127.0.0.1'])  # Enter the IPs of the ScyllaDB nodes
session = cluster.connect()

# Creating a keyspace (if it doesn't exist)
session.execute("""
CREATE KEYSPACE IF NOT EXISTS test_keyspace
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}
""")

# Selecting the keyspace
session.set_keyspace('test_keyspace')

Creating a Table

Now let's create a simple table to hold user data:

session.execute("""
CREATE TABLE IF NOT EXISTS users (
    id UUID PRIMARY KEY,
    name text,
    age int
)
""")

Inserting Data

We can use prepared statements to insert data:

import uuid

insert_stmt = session.prepare("INSERT INTO users (id, name, age) VALUES (?, ?, ?)")
session.execute(insert_stmt, (uuid.uuid4(), 'Alice', 30))
session.execute(insert_stmt, (uuid.uuid4(), 'Bob', 25))

Reading Data

Finally, let's read the data from the table:

rows = session.execute("SELECT id, name, age FROM users")
for row in rows:
    print(f"ID: {row.id}, Name: {row.name}, Age: {row.age}")

Conclusion

In this article, we covered the basic steps to connect to ScyllaDB using Python: connecting to the cluster, creating a keyspace and a table, inserting, and reading data. ScyllaDB, thanks to its compatibility with Cassandra and high performance, is an excellent choice for highly scalable distributed applications.

Back to top