4

我使用 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)

构建.sbt

name := "QuillSample"
version := "1.0"
scalaVersion := "2.12.0"
libraryDependencies ++= Seq(
   "io.getquill" % "quill-cassandra_2.11" % "1.0.0"
)

应用程序属性

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

我不确定需要哪些其他代码来满足缺失的隐式。

4

2 回答 2

5

Quill 还不支持 Scala 2.12。跟踪此 PR以了解进度。

改变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"
)
于 2016-11-27T13:45:18.737 回答
-1

所以看起来您没有能够构建的 SchemaMeta ,com.abhi.Movies因此您需要明确指定一个

implicit val movieSchemaMeta = schemaMeta[com.abhi.Movies](....)
于 2016-11-12T15:04:34.957 回答