16 min to complete
In two previous lessons, Coding with Java Part 1 and Part 2, we explained how to use the Java driver to create applications that interact with ScyllaDB. Division 3 has decided to explore the Java programming language a bit further and came across an interesting feature of ScyllaDB 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 ScyllaDB 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 ScyllaDB is a distributed system with fault protection and resiliency, storing files in ScyllaDB 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 set up the ScyllaDB cluster.
Starting the ScyllaDB Cluster
follow this procedure to remove previous clusters and set up a new cluster.
Next, create the keyspace catalog and the table mutant_data
docker exec -it scylla-node1 cqlsh
CREATE KEYSPACE catalog WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy','DC1' : 3};
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', 'http://www.facebook.com/bobloblaw');
insert into mutant_data ("first_name","last_name","address","picture_location") VALUES ('Bob','Zemuda','1202 Coffman Lane', 'http://www.facebook.com/bzemuda');
insert into mutant_data ("first_name","last_name","address","picture_location") VALUES ('Jim','Jeffries','1211 Hollywood Lane', 'http://www.facebook.com/jeffries');
The Java sample application that we are using was modified from the Datastax Blob example located on the Cassanda Driver’s GitHub page.
Building the Java Example
If you previously built the java Docker you can skip directly to “Running the Java Example”.
Otherwise, to build the application in Docker, change into the java subdirectory in scylla-code-samples:
exit
cd scylla-code-samples/mms/java
Now we can build and run the container:
docker build -t java-app .
To run the container and connect to the shell, run the following command:
docker run -d --net=mms_web --name some-java-app java-app
To connect to the shell of the container, run the following command:
docker exec -it some-java-app sh
Running the Java Example
Finally, the sample Java application can be run:
cd java-datatypes/target
java -jar DataTypes_App-jar-with-dependencies.jar
The output of the application will be the following (you can ignore the SLF4J warnings):
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 ScyllaDB 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 ScyllaDB 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:
exit
docker cp some-java-app:/tmp/Jim_Jefferies.png .
docker cp some-java-app:/tmp/Bob_Loblaw.png .
docker cp some-java-app:/tmp/Bob_Zemuda.png .
Using your favorite image viewer, you can verify that each image is correct.
Conclusion
In this lesson, we went over the different data types that someone can use in their database tables and learned how to store binary files in ScyllaDB with a simple Java application. By being able to store images in ScyllaDB, 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!
Note: If you are taking this lesson as part of the MMS course, keep in mind that there are more lessons on using drivers for other languages in the Using ScyllaDB Drivers course.