Getting Started Cockroach with Scala: An Introduction


Today we are going to discuss that how can we use the Scala with the Cockroach DB? As we all know that Cockroach DB is a distributed SQL database built on top of a transactional and consistent key-value store and now we are going to use it with the Scala. But before starting the journey, To those who have caught the train late,ūüėČ this is what has happened till now:

  1. An Introduction to CockroachDB !!

Now before starting the code please setup Cockroach DB on your local environment.

We have to follow these steps for setting up CockroachDB in your local environment.

  • We can download the Cockroach DB from here and follow the instruction which mentions there.
  • Now run the following commands for starting the Nodes:

# Start node 1:

cockroach start --insecure \
--store=CockroachTest-1 \
--host=localhost

# In a new terminal, start node 2:

cockroach start --insecure \
--store=CockroachTest-2 \
--host=localhost \
--port=26258 \
--http-port=8081 \
--join=localhost:26257

# In a new terminal, start node 3:

cockroach start --insecure \
--store=CockroachTest-3 \
--host=localhost \
--port=26259 \
--http-port=8082 \
--join=localhost:26257

Now your Cockroach DB is UP and in running conditions. Please check the localhost:8080 to see the Cockroach UI.

  • Now create the user:
cockroach user set maxroach --insecure
  • After creating the user, create Database and set Privilege
cockroach sql --insecure -e 'CREATE DATABASE test'

cockroach sql --insecure -e 'GRANT ALL ON DATABASE test TO maxroach'
  • Now Run the following commands for creating the table:
CREATE TABLE IF NOT EXISTS test.user (id VARCHAR(10) PRIMARY KEY, name TEXT, email VARCHAR(60));

After running all these commands, we are ready with our local environment on port 8080.

We will talk step by step to develop this Rest API after which you will able to run this API on your server and start hitting Rest end points.

When we start creating a project we have to use following dependencies:

  "com.typesafe.akka" %% "akka-http-core"   %   "10.0.7"
  "com.typesafe.slick"    %%  "slick"       %   "3.2.0",
  "com.typesafe.slick" %%  "slick-hikaricp" %   "3.2.0"1,
  "io.spray"       %%  "spray-json"         %   "1.3.3",
  "net.codingwell" %%  "scala-guice"        %   "4.1.0",
  "org.postgresql" %   "postgresql"         %   "9.4.1212",
  "io.spray"       %%  "spray-json"         % "1.3.3",
  "net.codingwell" %% "scala-guice"         % "4.1.0",
  • Create DB Component with Postgres and Slick for making the connection with Database:

  • Create the mapping for binding the data with the case class:

  • Create¬† User Component which will interact with Cockroach DB for performing the DB operations:

  • Create a user service which will work as a bridge between the Routes API and User Component:

  • Now finally create routes:

So this is the basic idea that how we can use Cockroach DB with scala.
You can get the above working example from the GitHub repo, checkout: GitHub
If You have any questions you can contact me here or on Twitter: @anuragknoldus


knoldus-advt-sticker


Advertisements
This entry was posted in Akka, akka-http, database, Scala, Slick and tagged . Bookmark the permalink.

One Response to Getting Started Cockroach with Scala: An Introduction

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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