Join us at ScyllaDB Labs, instructor-led, hands-on, training sessions | July 17
Register now

Coding with Python Part 1

10 min to complete

In a previous lesson, we explained how a ScyllaDB Administrator could backup and restore a cluster. As the number of mutants is on the rise, Division 3 decided that we must use more applications to connect to the mutant catalog and decided to hire Python developers to create powerful applications that can monitor the mutants. In this lesson, we will explore how to connect to a ScyllaDB cluster using the driver for Python.

When creating applications that communicate with a database such as ScyllaDB, the programming language being used must have support for database connectivity. Since ScyllaDB is compatible with Cassandra, we can use any of the available Cassandra libraries. For example, in Go, there is Gocql and Gocqlx. In Node.js, there is cassandra-driver. For Python, we have this driver available. Since Division 3 wants to start investing in Python, let’s begin by writing a sample Python application.

Creating a Sample Python Application

The sample application that we will go over will connect to a ScyllaDB cluster, display the contents of the Mutant Catalog table, insert and delete data, and show the contents of the table after each action. We will first go through each section of the code that will be used and then explain how to run the code in a Docker container that will access the ScyllaDB Mutant Monitoring cluster. You can see this file here: scylla-code-samples/mms/python/

In the application, we first need to import the necessary classes from the Python driver:

The Cluster class provides the ability to connect to the cluster. The ConsistencyLevel class will allow us to control the consistency level of the CQL queries that we are going to perform.

For this application, the main class is called App, and the code is stored in a file called In the App class constructor, we define which cluster, keyspace  and consistency level to use:

To display the data, we will need to create a function that will run a select statement from the ScyllaDB cluster. The function below will use the Session object to gather the data and print it on the screen:

The next function is used to insert a new Mutant into the Mutant Catalog table. Since this is a simple query, only the Session object is used. 

After the data is added and displayed in the terminal, we will delete it and then display the contents of the table again:

This is the main function of the application and is the starting point for any Python application. In this function, we will create the App object that we defined above and execute the methods in the order below. When all of the methods are completed, the application will exit and stop the Python driver.

With the coding part done, let’s set up the ScyllaDB Cluster and then run the sample application in Docker.

Setting up the ScyllaDB Cluster

The example requires a single DC cluster. Follow this procedure to remove previous clusters and set up a new cluster.

Once the cluster is up, we’ll create the catalog keyspace and populate it with data.

The first task is to create the keyspace for the catalog.

docker exec -it scylla-node1 cqlsh
CREATE KEYSPACE catalog WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy','DC1' : 3};

Now that the keyspace is created, it is time to create the table.

use catalog;
CREATE TABLE mutant_data (
   first_name text,
   last_name text,
   address text,
   picture_location text,
   PRIMARY KEY((first_name, last_name)));

Now let’s add a few mutants to the catalog with the following statements:

insert into mutant_data ("first_name","last_name","address","picture_location") VALUES ('Bob','Loblaw','1313 Mockingbird Lane', '');
insert into mutant_data ("first_name","last_name","address","picture_location") VALUES ('Bob','Zemuda','1202 Coffman Lane', '');
insert into mutant_data ("first_name","last_name","address","picture_location") VALUES ('Jim','Jeffries','1211 Hollywood Lane', '');

Building the Python Example

To build the application in Docker, change into the python subdirectory in scylla-code-samples:

cd scylla-code-samples/mms/python

Now we can build and run the container:

docker build -t python-app . 
docker run -d --net=mms_web --name some-python-app python-app

Viewing the Run Results

Once the Python app is run in the container, we can see the output by printing the logs:

docker logs some-python-app

The output of the application will be:


In this lesson, we explained how to create a sample Python application that executes a few basic CQL statements with a ScyllaDB cluster using the Python driver. This is only the basics, and there are more exciting topics that Division 3 wants developers to explore. In one of the next lessons, we will go over prepared statements using the Python driver.

In the meantime, please be safe out there and continue to monitor the mutants!