Getting Started With Phantom


Phantom is Reactive type-safe Scala driver for Apache Cassandra/Datastax Enterprise. So, first lets explore what Apache Cassandra is with some basic introduction to it.

Apache Cassandra

Apache Cassandra is a free, open source data storage system that was created at Facebook in 2008. It is highly scalable database designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure. It is a type of NoSQL database which is Schema-free. For more about Cassandra refer to this blog Getting Started With Cassandra.


We wanted to integrate Cassandra into Scala ecosystem that’s why we used Phantom-DSL as one of the module of outworkers. So, if you are planning on using Cassandra with Scala, phantom is the weapon of choice because of :-

  • Ease of use and quality coding.
  • Reducing code and boilerplate by at least 90%.
  • Automated schema generation

You don’t need to switch to CQL(Cassandra Query Langguage) to create keyspace in Cassandra.

So, for that first you need to add up the dependency into yout project :-

You need to import this by default in your code:-

Connecting to a Cassandra cluster

Now, you can connect to Cassandra cluster by creating a ContactPoint called Connector which is basically required to run the application. when we start creating databases, we pass in a ContactPoint or what we call a connector in more plain English.

You have added the dependency and connected to your Cassandra cluster now, You are ready to create your very first table. Let’s assume we are modelling the CQL schema for a case class that looks like this:-

Here’s how the Phantom DSL code looks like:-

where Users is the table and User is the case class. All the fields with their column type has been defined. There must be atleast one field which must be a Partition Key same as a Primary Key in SQL. Partition key tells how your data is partitioned across the cluster.

Earlier, we were using CassandraTable trait instead of Table trait that we have mentioned in the above Phantom DSL code. By extending Table trait you don’t need to extend the RootConnector explicitly as Table trait already extends CassandraTable with RootConnector.

So, the question is Why we needed to define RootConnector?

The simple answer for it is RootConnector defines the Keyspace and Session for you. It tells the table a connector with a `session` and `keySpace` will be injected later on.

You have created Keyspace and a table now, its time to insert data into the Cassandra table. You can do this by writing the below code snippet into the class Users.scala.

For quering your table, to get the data out of the table as needed by using various clauses and operators. Here, is the small code snippet :-

Hope you liked the blog. Happy coding!! 🙂




This entry was posted in Scala and tagged , , , , , . Bookmark the permalink.

3 Responses to Getting Started With Phantom

  1. Harry Klinton says:

    Awesome read. Just got here randomly and found this post very informative. Shared it with my friends.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s