Mutant Monitoring Systems (MMS) Day 9 – Interacting Programmatically
This is part 9 of a series of blog posts that provides a story arc for Scylla Training.
In the previous post, we setup the Scylla Monitoring stack for the Mutant Monitoring System so we can monitor the performance, latency, and status of nodes in the Scylla Cluster. Those of us in Division 3 now want to teach our development team how to create applications that can interact with the Scylla Cluster so we can build the next-generation tools for the Mutant Monitoring System. In this post, we will explore how to connect to a Scylla cluster using the available APIs.
When creating applications that communicate with a database such as Scylla, it is crucial that the programming language being used has support for database connectivity. Since Scylla is compatible with Cassandra, we can use any of the Cassandra libraries available. For example, in Go, there is Gocql and Gocqlx. In Node.js, there is cassandra-driver. Since Division 3 consists mostly of Node.js developers, let’s begin by writing a sample Node.js application.
Creating the Application
Switch to the terminal and create a new directory on your computer that can be used for this project. We will need to create a file called package.json. Package.json is used by the npm command to download the listed library dependencies such as cassandra-driver for this project. Using your favorite text editor, create the following file:
Next, we can begin coding the main application that will interact with the Mutant Monitoring System’s Scylla cluster. Using a text editor, create a new file called app.js. At the beginning of the file, we need to start by importing the cassandra-driver as shown below:
var cassandra = require('cassandra
In the next line, we can begin creating functions that will run queries on the Scylla cluster such as INSERT, DELETE, and SELECT in the tracking keyspace. The example below will insert data into the cluster with the following Strings: first_name, last_name, timestamp, heat, location, speed, telepathy_powers, date.
The client variable will create a new connection to the Scylla Cluster and also give the application the ability to communicate with all three nodes. By being able to specify multiple nodes, the application can be more resilient if a Scylla node fails, and provides performance by load-balancing the requests. The keyspace entry specifies the keyspace to use.
The query variable can be thought of as using cqlsh in your program. You can put any query that you want in there and have your application execute it on the Scylla cluster. By using ?’s in the VALUES section of the query variable, we can have the values populated by parameters which are specified in the next line. All of the parameters in the constant parms is what we gathered as input in the insertData function. When the query is executed with the client.execute call, you can see there is an entry for the query and parms that were set previously. If any errors are reported, they will be visible in the console.
With the application query done, we can now create the code that will call it and put data into it:
The setTimeout loop is used to randomly insert data every 50ms. Since Jim Jeffries is a popular mutant that we like to monitor at Division 3, we will manually put his information in for first_name, last_name, and location. The other required values use the Math and Date functions to automate the values. Finally, we call the insertData function that we added earlier to actually do the work. When the insert is complete, we will repeat the function. When the application starts, we call the load function loop with load();.
Now that that is done, we will need to get the Scylla Cluster up and running before we run the application.
Starting the Scylla Cluster
The Scylla Cluster should be up and running with the data imported from the previous blog posts.
The MMS Git repository has been updated to provide the ability to automatically import the keyspaces and data. If you have the Git repository cloned already, you can simply do a “git pull” in the scylla-code-samples directory.
git clone https://github.com/scylladb/scylla-code-samples.git
Modify docker-compose.yml and add the following line under the environment: section of scylla-node1:
Now we can build and run the containers:
docker-compose up -d
After roughly 60 seconds, the existing MMS data will be automatically imported. When the cluster is up and running, we can run our application code.
Running the Application
Since this application will run in Docker, we will need to create a file called Dockerfile to build and run the code. Using a text editor, paste the contents below into Dockerfile:
Next, run the following commands to build and run the application:
docker build -t app .
docker run -d --net=mms_web --name app app
docker logs -f app
We can see if everything is working properly by accessing the Scylla cluster and counting the rows:
docker exec -it mms_scylla-node1_1 cqlsh
select count(*) from tracking.tracking_data;
If you keep running the select count(*) query, you should be able to see the count increase every second.
In this post, we explained how developers at Division 3 can create applications that interact with the Mutant Monitoring System by showing them how to use the Cassandra libraries available in programming languages. Since Scylla is compatible with Cassandra, developers coming from Cassandra will not have to make changes to their existing code. Please continue to innovate to help Division 3 create new tools that can track Mutants and keep the public safe from tyranny.
- Learn more about Scylla from our product page.
- See what our users are saying about Scylla.
- Download Scylla. Check out our download page to run Scylla on AWS, install it locally in a Virtual Machine, or run it in Docker.
- Take Scylla for a Test drive. Our Test Drive lets you quickly spin-up a running cluster of Scylla so you can see for yourself how it performs.