Scylla and Python

Installing the driver

Scylla supports the same set of drivers that are compatible with CQL/Cassandra, so to connect your python application, use the DataStax Python Driver. Installation can be done through pip:
pip install cassandra-driver

SSL and Authentication

Since Compose Scylla deployments are SSL-enabled, you will need to make sure that you are suppling the information via python that the server needs to validate the client. You will need to:

  • import the ssl package
  • download the certificate from Scylla and certificates and save it locally
  • Supply the SSL options, including the path to the CA certificate in your application.
ssl_options = {
    'ca_certs': '/path/to/lechain.crt',
    'ssl_version': ssl.PROTOCOL_TLSv1_2
}

Username and password credentials are supplied via class that works with Cassandra's PasswordAuthenticator, in this case a plaintext username and password, which are both supplied by Compose and can be found in the Connection Info on your Deployment Overview.

auth_provider = PlainTextAuthProvider(
                username='scylla',
                password='<password>')

Connecting

You supply the connection information to the Cluster command, containing the information from the Connection Strings provided with your deployment. contact_points is an array that looks for a connection to one of the three nodes, port supplies the port number, and auth_provider and ssl_options supply the previously configured security requirements.

Next, start a new session and tell it which keyspace to use. Here, we used the keyspace set up via cqlsh in the Scylla and cqlsh example.

cluster = Cluster(
            contact_points = ['portal880-29.vivid-scylla-25.compose-34.composedb.com',\
                  'portal924-27.vivid-scylla-25.compose-34.composedb.com',\
                  'portal1242-25.vivid-scylla-25.compose-34.composedb.com'],
            port = 23376,
            auth_provider = auth_provider,
            ssl_options=ssl_options)

# Starts session, connects to a keyspace
session = cluster.connect('example_keyspace')

Full Example Code

The sample transaction inserts a name into a table that contians a column for first names. last names, and a UUID primary key. If the 'names' table doesn't already exist, you can create one via cqlsh or the data browser.

from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
import ssl
import uuid

# Optional dict with absolute path to CA certificate and the defualt Cassandra protocol ssl version.
ssl_options = {
    'ca_certs': '/path/to/lechain.crt',
    'ssl_version': ssl.PROTOCOL_TLSv1_2
}

# Creates class object that supplies username/password in plain-text.
auth_provider = PlainTextAuthProvider(
                username='scylla',
                password='<password>')

# Handles connection setup and information
cluster = Cluster(
            contact_points = ['portal880-29.vivid-scylla-25.compose-34.composedb.com',\
                  'portal924-27.vivid-scylla-25.compose-34.composedb.com',\
                  'portal1242-25.vivid-scylla-25.compose-34.composedb.com'],
            port = 23376,
            auth_provider = auth_provider,
            ssl_options=ssl_options)

# Starts session, connects to a keyspace
session = cluster.connect('example_keyspace')

# Transaction, inserting a new name into table
new_name_insert = session.prepare("""
    INSERT INTO names(name_id, first_name, last_name)
    VALUES (?, ?, ?)""")

# Executes transaction, contains values to be inserted
session.execute(new_name_insert, [uuid.uuid4(), 'Charlie', 'Button'])

More Information


Still Need Help?

If this article didn't solve things, summon a human and get some help!