Golang and ScyllaDB Part 2 – Data Types

12 min to complete

In a previous lesson, Golang and ScyllaDB Part 1, we explained how to use the Golang driver to create applications that interact with ScyllaDB. Division 3 has decided to explore the Golang programming language a bit further and came across an exciting 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 Golang 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 Golang sample application that we are using was modified from the Datastax Blob example located on the Cassandra Driver’s GitHub page.

Building the Golang Example

If you previously built the Golang  Docker, you can skip directly to “Running the Golang Example.”

Otherwise, to build the application in Docker, change into the “mms/go” subdirectory in scylla-code-samples:

exit
cd scylla-code-samples/mms/go

Now we can build and run the container:

docker build -t go-app .

To run the container and connect to the shell, run the following command:

docker run -d --net=mms_web --name some-go-app go-app

To connect to the shell of the container, run the following command:

docker exec -it some-go-app sh

Running the Golang Example

Finally, the sample Golang application can be run:

godatatypes

The output of the application will be:

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 Golang application and stored in ScyllaDB according to their names.

This code reads the file and inserts it into ScyllaDB:

The following function will read the file from ScyllaDB, convert it to ASCII, and save it as a file in /tmp.

Finally, it all comes together in the main function, which for each mutant: reads the file, inserts it into ScyllaDB, reads the file from ScyllaDB, and saves it.

To retrieve the images from the container to verify that everything worked correctly, 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-go-app:/tmp/Jim_Jefferies.jpg .
docker cp some-go-app:/tmp/Bob_Loblaw.jpg .
docker cp some-go-app:/tmp/Bob_Zemuda.jpg .

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 Golang 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!

 

fa-angle-up