Permalink
Browse files

Introduced Cassandra driver

  • Loading branch information...
1 parent 3a96b61 commit 0fa30e4874b1398f1557dbddd2346393d15c41e3 @manuelkiessling manuelkiessling committed Jan 18, 2016
Showing with 40 additions and 1 deletion.
  1. +19 −0 app/cassandra/CassandraConnectionUri.scala
  2. +19 −0 app/cassandra/CassandraConnector.scala
  3. +2 −1 build.sbt
@@ -0,0 +1,19 @@
+package cassandra
+
+import java.net.URI
+
+case class CassandraConnectionUri(connectionString: String) {
+
+ private val uri = new URI(connectionString)
+
+ private val additionalHosts = Option(uri.getQuery) match {
+ case Some(query) => query.split('&').map(_.split('=')).filter(param => param(0) == "host").map(param => param(1)).toSeq
+ case None => Seq.empty
+ }
+
+ val host = uri.getHost
+ val hosts = Seq(uri.getHost) ++ additionalHosts
+ val port = uri.getPort
+ val keyspace = uri.getPath.substring(1)
+
+}
@@ -0,0 +1,19 @@
+package cassandra
+
+import com.datastax.driver.core._
+
+object CassandraConnector {
+
+ def createSessionAndInitKeyspace(uri: CassandraConnectionUri,
+ defaultConsistencyLevel: ConsistencyLevel = QueryOptions.DEFAULT_CONSISTENCY_LEVEL) = {
+ val cluster = new Cluster.Builder().
+ addContactPoints(uri.hosts.toArray: _*).
+ withPort(uri.port).
+ withQueryOptions(new QueryOptions().setConsistencyLevel(defaultConsistencyLevel)).build
+
+ val session = cluster.connect
+ session.execute(s"USE ${uri.keyspace}")
+ session
+ }
+
+}
View
@@ -10,7 +10,8 @@ libraryDependencies ++= Seq(
jdbc, jdbc,
cache, cache,
ws, ws,
- "org.scalatestplus" %% "play" % "1.4.0-M4" % "test" + "org.scalatestplus" %% "play" % "1.4.0-M4" % "test",
+ "com.datastax.cassandra" % "cassandra-driver-core" % "2.1.+"
) )
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases" resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"

0 comments on commit 0fa30e4

Please sign in to comment.