我在我的“play-scala”项目中使用quill作为cassandra驱动程序。 我有一个结构如下的表 -
CREATE TABLE user_data (
id int,
name string,
addresses list<frozen<dwelltimebd>>
PRIMARY KEY ((id, name))
)
其中ADDRESS是用户定义的数据类型,如此处所述 -
CREATE TYPE ADDRESS (
city string,
country string
);
为访问此表中的数据而编写的代码是这样的 -
object UserTable {
case class addresses(city: String, country: String)
case class userData ( id :Int, name :String, addresses : Seq[addresses])
lazy val ctx = new CassandraAsyncContext[SnakeCase]("user")
import ctx._
implicit val seqAddressDecoder: Decoder[Seq[addresses]] =
decoder[Seq[addresses]] { (row: Row) =>
(index) =>
row.getList(index, classOf[addresses]).asScala
}
implicit val seqAddressesEncoder: Encoder[Seq[addresses]] =
encoder[Seq[addresses]] { (row: BoundStatement) =>(idx, lista) =>
row.setList(idx, lista.toList.asJava, classOf[addresses])
}
def getUserData(id: Int, name: String) = {
val getAllDetail = quote {
query[userData].filter(p => p.id == lift(id) && p.name == lift(name))
}
val result: List[userData] = Await.result(ctx.run(
getAllDetail
), Duration.Inf)
result
}
}
在运行上述代码时,收到以下错误, -
play.api.UnexpectedException: Unexpected exception[CodecNotFoundException: Codec not found for requested operation: [frozen<user.addresses> <-> models.databaseModels.UserTable$addresses]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:289)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220)
at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:346)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:345)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [frozen<user.addresses> <-> models.databaseModels.UserTable$addresses]
at com.datastax.driver.core.CodecRegistry.notFound(CodecRegistry.java:679)
at com.datastax.driver.core.CodecRegistry.createCodec(CodecRegistry.java:526)
at com.datastax.driver.core.CodecRegistry.findCodec(CodecRegistry.java:506)
at com.datastax.driver.core.CodecRegistry.maybeCreateCodec(CodecRegistry.java:558)
at com.datastax.driver.core.CodecRegistry.createCodec(CodecRegistry.java:524)
at com.datastax.driver.core.CodecRegistry.findCodec(CodecRegistry.java:506)
at com.datastax.driver.core.CodecRegistry.access$200(CodecRegistry.java:140)
at com.datastax.driver.core.CodecRegistry$TypeCodecCacheLoader.load(CodecRegistry.java:211)
at com.datastax.driver.core.CodecRegistry$TypeCodecCacheLoader.load(CodecRegistry.java:208)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3542)
无法解决问题,我收到的一些指示是,
I need to first create Java TypeCodec and register it with the cluster. Then also need to implement row-codec for quill just to make it compile.
无法理解如何做到这一点,这方面的任何帮助都会有所帮助。
答案 0 :(得分:2)
这对我有用:
来自Datastax Docs:
默认情况下,驱动程序将用户定义的类型值映射到UDTValue 实例
对于解码器尝试以下内容:
import scala.collection.JavaConverters._
implicit val addressListDecoder: Decoder[List[Address]] = decoder(
(index, row) =>
row.getList(index, classOf[UDTValue]).asScala.toList.map { a =>
Address(a.getString("city"), a.getString("country"))
}
)
这不起作用:(但也许我没有努力尝试)
理论上你也可以定义自定义编解码器,扩展Quill的CassandraAsyncContext
(或任何可用的上下文)以获得Cluster
个对象,然后注册自定义编解码器:
def registerCodecs(cluster: Cluster): Unit = {
val codecRegistry = cluster.getConfiguration.getCodecRegistry
codecRegistry.register(new LocalDateTimeCodec) //just for example
}
您可以从现有的“基本”编解码器编写编解码器。