Golang and ScyllaDB Part 1

9 min to complete

Previously we saw how to interact with ScyllaDB using the Java driver. 

In this lesson, we will see how to do so using the Go Programming Language, also known as Golang. 

Go is an open-source programming language designed at Google in 2007. It’s syntactically similar to C but with garbage collection, structural typing, memory safety, and communicating sequential processes style concurrency. 

GoCQL is a ScyllaDB client for the GO language. GoCQLX is an extension to GoCQL that boosts developer productivity while not sacrificing query performance.

For a deep dive into using Go with ScyllaDB, check out this blog post. 

Creating a Sample Golang Application

The sample application that we will go over is available on GitHub. We will connect to a ScyllaDB cluster, display the contents of the Mutant Catalog table, insert and delete data, and show the contents of the table after each action. We will first go through each section of the code that will be used and then explain how to run the code in a Docker container that will access the ScyllaDB Mutant Monitoring cluster. 

For this application, the main class is called main, and the code is stored in a file called main.go. It’s located here: scylla-code-samples/mms/go/cmd/goapp/main.go

We’ll start by going over the code. In the application, we first need to import the driver for Go:

The driver provides the ability to connect to the cluster and perform different queries.

After the class is defined, we define which cluster and keyspace to use, and we connect to the cluster:

To display the data, we will need to create a function that will run a SELECT statement from the ScyllaDB cluster. This function is defined in the file scylla-code-samples/mms/go/internal/scylla/query.go

The function performs the SELECT query and prints the data using the logger:

The next function is used to insert a new Mutant into the Mutant Catalog table. This function is defined in the file main.go as well.

After the data is added, we call on the SelectQuery function again to make sure that the data was actually added. 

After the data is added and displayed in the terminal, we will delete it and then display the contents of the table again. The deleteQuery is also defined in the file main.go:

Now we execute the SelectQuery function again to make sure that the data was deleted.

With the coding part done, let’s set up the ScyllaDB Cluster and then run the sample application in Docker.

Setting up the ScyllaDB Cluster

The example requires a single DC cluster. Follow this procedure to remove previous clusters and set up a new cluster.

Once the cluster is up, we’ll create the catalog keyspace and populate it with data.

The first task is to create the keyspace for the catalog.

docker exec -it scylla-node1 cqlsh
CREATE KEYSPACE catalog WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy','DC1' : 3};

Now that the keyspace is created, it is time to create the table.

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');

Building the Golang Example

Exit the CQL Shell if you’re still in it:

exit

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

cd scylla-code-samples/mms/go

Now we can build and run the container:

docker build -t go-app .
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:

goapp

The output of the application will be:

Conclusion

In this lesson, we explained how to create a sample Golang application that executes a few basic CQL statements with a ScyllaDB cluster using the GoCQL driver. This is only the basics, and there are more exciting topics that Division 3 wants developers to explore. In the next lessons, coming soon, we will go over more advanced topics using Go and ScyllaDB.

In the meantime, please be safe out there and continue to monitor the mutants!

fa-angle-up