Getting Started Cockroach with Scala: An Introduction

Knoldus Inc.
Jul 23, 2017 · 3 min read

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:
https://gist.github.com/anuragknoldus/154506f09db7901b9bbbac69298191c7https://gist.github.com/anuragknoldus/5bfc66943a01d42a0815636f7b95950e
  • Create the mapping for binding the data with the case class:
https://gist.github.com/anuragknoldus/fd224114d7b62a5f72cf1bc91c91ff2a
  • Create User Component which will interact with Cockroach DB for performing the DB operations:
https://gist.github.com/anuragknoldus/d20920047d6859d70067cbd0bc6e992e
  • Create a user service which will work as a bridge between the Routes API and User Component:
https://gist.github.com/anuragknoldus/425beabd6caf1194e1f48f2a7df4cec6
  • Now finally create routes:
https://gist.github.com/anuragknoldus/ee1e4c1a3136a5ce2a9698c377c1bfbaSo 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
knoldus-advt-sticker

Knoldus - Technical Insights

Knols, Insights and Opinions from the curious minds at Knoldus Inc.

Knoldus Inc.

Written by

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com

Knoldus - Technical Insights

Knols, Insights and Opinions from the curious minds at Knoldus Inc.

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