我尝试使用幻像scala驱动程序连接到cassandra数据库(使用scala 2.11.2)
我在他们的博客上关注了这篇文章: http://blog.websudos.com/2014/08/a-series-on-cassandra-part-1-getting-rid-of-the-sql-mentality/
(注意github上只有2.11编译的幻影-dl jar,我不知道是否有问题?)
我只有一个与幽灵的依赖
<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>
当我编译项目时,我收到有关会话的错误:
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] ------------------------------------------------------------------------
在他们的github上,有一个会话示例:
implicit val session = SomeCassandraClient.session;
但我不明白SomeCassandraClient的位置?
有什么建议吗?
答案 0 :(得分:2)
您可以从连接器(您正在寻找的&#39; SomeCassandraClient)中获取会话。
您已经在某处定义了连接器,如:
trait Connector extends SimpleCassandraConnector {
val keySpace = "your_keyspace"
// other connection params as needed
}
object Connector extends Connector
然后只做一个
implicit val session = Connector.session
这样你就不必多次定义连接IP和密钥空间; - )
答案 1 :(得分:2)
定义幻像表时,一种非常常见的做法是使用简单的特征注入会话。这正是我们以这种方式创建连接器的原因,因此它们可以通过特性mixin / inheritance自动提供会话。
import com.websudos.phantom.connectors.SimpleCassandraConnector
trait MyConnector extends SimpleCassandraConnector {
override val keySpace = "whatever"
}
现在,当您定义class MyTable
时,您应该定义:
object MyTable extends MyTable with MyConnector {
def getById(id: String): Future[Option[..]] {
}
}
您还应该使用较新版本的Phantom,分别为1.5.0。 Scala 2.11支持在1.2.7中相对较新,因此可能存在奇怪的问题,但所有这些问题现在已经修复。
您也不必担心正在创建多个对象。使用连接器时,底层的Cassandra连接实际上是全局连接,并且所有相关锁都已就位。你正在做的是为你的所有表提供完全相同的会话,即使它可能不会出现。