Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Introduced Cassandra driver
- Loading branch information
1 parent
3a96b61
commit 0fa30e4
Showing
3 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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) | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters