Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phantom-dsl_2.11 error implicit session

I'm trying to connect to the cassandra database (With scala 2.11.2) using the phantom scala driver

I followed this article on their blog: http://blog.websudos.com/2014/08/a-series-on-cassandra-part-1-getting-rid-of-the-sql-mentality/

(note on github there is only phantom-dsl jar compiled in 2.11, I don't know if a problem?)

I've only one dependency with phantom

    <dependency>
        <groupId>com.websudos</groupId>
        <artifactId>phantom-dsl_2.11</artifactId>
        <version>1.2.7</version>
    </dependency>
    <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-core</artifactId>
        <version>2.0.1</version>
    </dependency>

When I compile my project, I get this error about session :

Main.scala:32: error: could not find implicit value for parameter session: com.datastax.driver.core.Session
[ERROR]       select.where(_.firstName eqs firstName).limit(5000).fetch()
[ERROR]                                                                ^
[ERROR] one error found
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

On their github, there is an example with a session :

  implicit val session = SomeCassandraClient.session;

But I don't understand where SomeCassandraClient is located ?

Any advice ?

like image 628
Guillaume Avatar asked Sep 29 '22 05:09

Guillaume


2 Answers

You can take the session from your connector (which is the 'SomeCassandraClient you are looking for).

Somewhere you have defined your connector like:

trait Connector extends SimpleCassandraConnector {
    val keySpace = "your_keyspace"
    // other connection params as needed
}
object Connector extends Connector

then just do a

implicit val session = Connector.session

That way you don't have to define multiple time your connection IP and keyspace ;-)

like image 162
Martin Avatar answered Oct 13 '22 11:10

Martin


When you define a phantom table, a very common practice is to inject the session with a simple trait. That's exactly why we created connectors in that fashion, so they can automatically provide the session via trait mixin/inheritance.

import com.websudos.phantom.connectors.SimpleCassandraConnector

trait MyConnector extends SimpleCassandraConnector {
  override val keySpace = "whatever"
}

Now when you define the class MyTable, you should then define:

object MyTable extends MyTable with MyConnector {
  def getById(id: String): Future[Option[..]] {
  }
}

You should also use a newer version of Phantom, respectively 1.5.0. Scala 2.11 support was relatively new in 1.2.7, so there may be strange issues because of that, but all of them have now been fixed.

You also don't need to worry about multiple objects being created. When using connectors, the underlying Cassandra connection is actually global, with all the relevant locks in place. What you are doing is providing the exact same session to all your tables, even if it may not appear so.

like image 26
flavian Avatar answered Oct 13 '22 10:10

flavian