Mutant Monitoring System Day 15 – Coding with Java Part 3
This is part 15 of a series of blog posts that provides a story arc for Scylla Training.
In the previous post, we explained how to do data analytics on a Scylla cluster using Apache Spark, Hive, and Superset. Division 3 has decided to explore the Java programming language a bit further and came across an interesting feature of Scylla that would allow us to store files in the database. With this ability, we can store images of the mutants in the catalog keyspace. With the images stored, Division 3 can see what the mutant looks like whenever they want and even share the image and tracking details with local law enforcement officials if needed.
A table in Scylla supports a wide array of data types such as timestamp, text, integer, UUID, blob, and more. The blob datatype stores binary data into a table. For Division 3’s use case, we will add a blob column to the catalog.mutant_data table and store images for each mutant there using a Java application. Since Scylla is a distributed system with fault protection and resiliency, storing files in Scylla will have the same benefits as our existing data based on the replication factor of the keyspace. To get started, we will first need to bring up the Scylla cluster.
Starting the Scylla Cluster
The MMS Git repository has been updated to provide the ability to automatically import the keyspaces and data. If you have the Git repository already cloned, 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.
Building and Running the Java Example
The Java sample application that we are using was modified from the Datastax Blob example located on the Cassanda Driver’s GitHub page. To build the application in Docker, change into the java-datatypes subdirectory in scylla-code-samples:
Now we can build and run the container:
docker build -t java .
To run the container and connect to the shell, run the following command:
docker run -it --net=mms_web --name java java sh
Finally, the sample Java application can be run:
java -jar App.jar
The output of the application will be the following:
Let’s dive a little bit deeper to see what the code is doing. When the application is run, it will add two columns to the catalog.mutant_data table: b and m. Column b is the blob column where the binary file is stored and column m is used to record the file’s name.
In the container, there is an image file for each mutant that will be read by the Java application and stored in Scylla according to their names.
The following functions will read the file and store it in a memory buffer and then insert them into the table using a prepared statement:
The final step for the Java application is to read the data from Scylla and write the data to /tmp. The select query used fetches the blob column and sorts it by the primary keys (first_name and last_name).
To retrieve the images from the container to verify that everything worked properly, we can run the following Docker commands to copy the newly written images out of the container and to your machine:
Using your favorite image viewer, you can verify that each image is correct.
In this blog post, we went over the different data types that someone can use in their database tables and learned how to store binary files in Scylla with a simple Java application. By being able to store images in Scylla, Division 3 will be able to quickly see what a mutant looks like and can share the details with local law enforcement if needed. With the ability to store files in the Mutant Monitoring System, the possibilities are endless for how Division 3 can continue to evolve the system. Please be safe out there and continue to monitor the mutants!