Cassandra scala client

Happy searchOps

Sep 14 · 3 min read

Cassandra prepared statements

Cassandra supports prepared statements, which can compile once and run multiple times. When prepare the statement, Cassandra will parse the query string, cache the result and return a unique identifier (the PreparedStatement object keeps an internal reference to that identifier). When you bind and execute a prepared statement, the driver will only send the identifier, which allows Cassandra to skip the parsing phase.

In this post I will discuss about how to use cassandra prepared statements with scala to do query operations. On my previous post I have discussed about using CQL queries to do querying from cassandra. All source codes which relates to this post available on gitlab. Please clone the repository and continue the post.

Run Cassandra

I have run Cassandra with Elassandra docker image. Elassandra built by combining Elasticseach with Cassandra. It comes Elasticsearch as a Cassandra plugin. Basically it has Cassandra API as well as Elasticsearch API. When data save on Cassandra it will automatically index on Elasticsearch. Read more about elassandra from here. Following is the docker-compose.yml to run the elassandra.

Now I can run the Elassandra with following commands. It will start Cassandra on my local machine. I can access the Cassandra via Cqlsh inside the Elassandra docker container.

Cassandra schemas

In cassandra I have a keyspace named mystiko and table a named documents. documents table contains a UDT SET named signatures. Following are the schemas.

Sbt dependency

I’m using IntelliJ Idea as my IDE to work with Scala applications. I need to create sbt project and add the build.sbt dependency file with cassandra and other dependencies. Following is the build.sbt dependency file.

Cassandra config

The cassandra configurations defined in cassandra.conf file. It contains host, port, keyspace etc informations.

These configs load into CassandraConf.scala trait and use to create the cassandra session.

Cassandra cluster

Then I need to create a cassandra session which can use to execute queries. I have defined cluster and poolingOptions here.

Document protocol

I have defined the document and signatures in cassandra schema as case class objects. These objects used when creating/querying the documents on cassandra.

Create document

To create a document, I have bind the values to create document prepared statement and execute it. Since document contains Singnature UDT SET, I need to convert List[Singature] into java.util.Set[UDTValue].

Get document

When querying the document I need to convert Signature UDTValue SET into scala List[Signature].

Update document

Following is the prepared statement and function to use update the document. It simply binds update fields in prepared statement and execute.

Add/remove tag

tags field is a TEXT type SET in cassandra. Following are the prepared statements and functions to add/remove tags to the document.

Add/remove signature

signatures field is a UDT type SET in cassandra. Cassandra does not support to bind prepared statements in UDT type collections. Due to this reason I have used QueryBuilder to add/remove signatures into the document.




Written by


Scala, Golang with Vim and Hockey: What else does a man need to be happy :)



Have less, be more

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade