|IMPORTANT: Since the first publication of the Mutant Monitoring System we have made a number of updates to the concepts and code presented in the blog series. You can find the latest version now in ScyllaDB University. Click here to be directed to the new version.
This is part 9 of a series of blog posts that provides a story arc for ScyllaDB Training.
In the previous post, we setup the ScyllaDB Monitoring stack for the Mutant Monitoring System so we can monitor the performance, latency, and status of nodes in the ScyllaDB Cluster. Those of us in Division 3 now want to teach our development team how to create applications that can interact with the ScyllaDB 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 ScyllaDB cluster using the available APIs.
When creating applications that communicate with a database such as ScyllaDB, it is crucial that the programming language being used has support for database connectivity. Since ScyllaDB 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 ScyllaDB 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 ScyllaDB 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 ScyllaDB 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 ScyllaDB 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 ScyllaDB 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 ScyllaDB Cluster up and running before we run the application.
Starting the ScyllaDB Cluster
The ScyllaDB 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 ScyllaDB 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 ScyllaDB 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 ScyllaDB from our product page.
- See what our users are saying about ScyllaDB.
- Download ScyllaDB. Check out our download page to run ScyllaDB on AWS, install it locally in a Virtual Machine, or run it in Docker.
- Take ScyllaDB for a Test drive. Our Test Drive lets you quickly spin-up a running cluster of ScyllaDB so you can see for yourself how it performs.