基于activator-akka-cassandra示例,我正在创建自己的应用程序,将数据保存到cassandra中.
我将数据模型定义如下
import play.api.libs.json.Json
case class Location(lat: Double, long: Double)
object Location {
implicit def toLocation(lat: Double, long: Double) = Location(lat, long)
}
case class LocationWithTime(location: Location, time: Long)
object LocationWithTime {
implicit def toLocationWithTime(location: Location, time: Long) = LocationWithTime(location, time)
}
case class Ping(pingUuid: String, senPhoneNumber: String, recPhoneNumber: Set[String], locationWithTime: LocationWithTime)
object Ping {
implicit val LocationFormat = Json.format[Location]
implicit val LocationWithTimeFormat = Json.format[LocationWithTime]
implicit val PingFormat = Json.format[Ping]
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,应该保存数据的代码:
def insertPing(ping: Ping): Unit =
session.executeAsync(insertPing.bind(ping.pingUuid, ping.senPhoneNumber, ping.recPhoneNumber,
ping.locationWithTime.location.lat, ping.locationWithTime.location.long, ping.locationWithTime.time))
Run Code Online (Sandbox Code Playgroud)
不编译并返回错误
Error:(24, 38) the result type of an implicit conversion must be more specific than AnyRef
ping.locationWithTime.location.lat, ping.locationWithTime.location.long, ping.locationWithTime.time))
^
Run Code Online (Sandbox Code Playgroud)
示例数据模型中的案例类扩展了AnyVal.基于此,我猜想类似的东西也会在我的情况下做到,但我不能这样做,因为ValueClasses只能有一个参数.我该如何解决这个问题?
虽然我不熟悉Cassandra,但我认为你的问题是bind需要Java Object(即AnyRefs),而且lat是scala.Double(即Java double),而不是AnyRef.
您可以通过包装你让这样的scala.Double以s java.lang.Double.
insertPing.bind(ping.pingUuid, ping.senPhoneNumber, ping.recPhoneNumber, new java.lang.Double(ping.locationWithTime.location.lat), new java.lang.Double(ping.locationWithTime.location.long), ping.locationWithTime.time)
Run Code Online (Sandbox Code Playgroud)