我使用Quill库编写了这个简单的应用程序来查询Cassandra
QuillSample.scala
import java.util.UUID
import io.getquill._
import scala.concurrent.ExecutionContext.Implicits.global
object QuillSample extends App {
lazy val ctx = new CassandraSyncContext[SnakeCase]("ctx")
import ctx._
val getAllRows = quote {
query[Movies]
}
val result = ctx.run(getAllRows)
println(result)
}
case class Movies(uUID: UUID,
avgRating: Float,
genres: Set[String],
name: String,
releaseDate: java.util.Date,
videoReleaseDate: java.util.Date)
build.sbt
name := "QuillSample"
version := "1.0"
scalaVersion := "2.12.0"
libraryDependencies ++= Seq(
"io.getquill" % "quill-cassandra_2.11" % "1.0.0"
)
application.properties
ctx.keyspace=movielens_small
ctx.preparedStatementCacheSize=1000
ctx.session.contactPoint=192.168.1.169
ctx.session.withPort=9042
ctx.session.queryOptions.consistencyLevel=LOCAL_QUORUM
ctx.session.withoutMetrics=true
ctx.session.withoutJMXReporting=false
ctx.session.maxSchemaAgreementWaitSeconds=1
ctx.session.addressTranslater=com.datastax.driver.core.policies.IdentityTranslator
返回编译时错误
Error:(14, 12) Can't find an implicit `SchemaMeta` for type `com.abhi.Movies`
query[Movies]
基于这里给出的文档
https://github.com/getquill/quill/blob/master/CASSANDRA.md我不确定还需要什么代码来满足缺失的隐式
Quill还不支持Scala 2.12。跟踪此PR的进度。
Change scalaVersion
:
scalaVersion := "2.11.8"
还必须使用快照依赖,因为这个修复是在1.0.0版本之后进行的:
resolvers ++= Seq(
Resolver.sonatypeRepo("snapshots")
)
libraryDependencies ++= Seq(
"io.getquill" %% "quill-cassandra" % "1.0.2-SNAPSHOT"
)
接下来,您必须为scala.collection.Set[T]
实现自定义编码器和解码器:
trait Encoders {
this: CassandraSessionContext[_] =>
implicit def setEncoder[T](implicit t: ClassTag[T]): Encoder[Set[T]] =
encoder((index, value, row) => row.setSet(index, value.asJava, t.runtimeClass.asInstanceOf[Class[T]]))
}
trait Decoders {
this: CassandraSessionContext[_] =>
implicit def setDecoder[T](implicit t: ClassTag[T]): Decoder[Set[T]] =
decoder((index, row) => row.getSet(index, t.runtimeClass.asInstanceOf[Class[T]]).asScala.toSet)
}
并将其混合在你的上下文中:
lazy val ctx = new CassandraSyncContext[SnakeCase]("ctx") with Encoders with Decoders
你可能会得到一个警告:
未找到javax.annotation.Nullable类-继续使用存根。
要修复它,添加以下依赖项:
libraryDependencies ++= Seq(
"com.google.code.findbugs" % "jsr305" % "3.0.1"
)
所以看起来你没有一个能够构建com.abhi.Movies
的SchemaMeta所以你需要显式指定一个
implicit val movieSchemaMeta = schemaMeta[com.abhi.Movies](....)