Cassandra scala client with Quill

Happy searchOps

(λx.x)eranga
Effectz.AI
2 min readOct 11, 2019

--

About Quill

Quill provides Scala based Quoted Domain Specific Language(QDSL) which supports compile time query generation and validation. It allows to express queries in Scala and access database similar to Scala collections. Quill is designed to support multiple target languages. At the moment it supports SQL and CQL(Cassandra Query Language).

In this post I’m gonna discuss about using Quill to do query operations with cassandra. In my previous post I have discussed about using cassandra prepared statements with scala. 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 document. document 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 quill-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 have created 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.

UDT mapping

document table contains UDT type signature. In order to query signature type I have defined custom encoder and decoder. It converts signature case class objects into UDT signatures and UDT signatures in to signature case class objects.

Document store

Then I have defined DocumentStore which facilitates document create, update, search query functions with Quill.

Test application

Finally I have run the create, update, search query functions which defined in DocumentStore. Following is the Main application with query outputs.

Reference

  1. https://getquill.io/#quotation-introduction
  2. http://pawelgebal.com/articles/quill/
  3. https://scalac.io/quill-compile-time-queries/
  4. https://softwaremill.com/comparing-scala-relational-database-access-libraries/
  5. http://homepages.inf.ed.ac.uk/wadler/papers/qdsl/qdsl.pdf
  6. https://medium.com/rahasak/cassandra-scala-client-d50ebd5a9723

--

--