我正在尝试执行以下操作但由于某些原因,由于KeySerializer的类型不匹配,我的代码将无法编译。
implicit val keyspace = ConnectionPool.bigdataKeyspace
implicit val CF_PAST = ColumnFamily.newColumnFamily(CF, LongSerializer.get, StringSerializer.get)
def update(model: M) = {
val batch = keyspace.prepareMutationBatch().setConsistencyLevel(ConnectionPool.CL_WRITE)
val rk = model.rowkey
try {
batch.withRow(CF_PAST, rk)
.putColumnIfNotNull(model.epoch, model.value, model.ttl)
batch.execute
Option(model)
} catch {
case e: Exception =>
logger.warn("Unable to insert past model", e)
None
}
}
编译错误如下“
error: type mismatch; found
: com.netflix.astyanax.model.ColumnFamily[Long,String] required: com.netflix.astyanax.model.ColumnFamily[Comparable[_ >:
String with Long <: Comparable[_ >: String with Long <:
Comparable[_ >: String with Long <:
java.io.Serializable] with java.io.Serializable] with java.io.Serializable] with java.io.Serializable,String] Note:
Long <: Comparable[_ >: String with Long <: Comparable[_ >:
String with Long <: Comparable[_ >: String with Long <:
java.io.Serializable] with java.io.Serializable] with java.io.Serializable] with java.io.Serializable, but Java-defined class ColumnFamily is invariant in type K.
You may wish to investigate a wildcard type such as `_ <:
Comparable[_ >: String with Long <: Comparable[_ >:
String with Long <: Comparable[_ >: String with Long <: java.io.Serializable] with
java.io.Serializable] with java.io.Serializable] with java.io.Serializable`.
(SLS 3.2.10)
batch.withRow(CF_PAST, rk)
这似乎只发生在Scala中,KeySerializers不是String,ColumnSerializers,因为其他类型似乎工作正常?有解决方法吗?
答案 0 :(得分:0)
啊我意识到KeySerializer对于这种情况是不正确的。我需要设置ColumnSerializer:
implicit val CF_PAST = ColumnFamily.newColumnFamily(CF, StringSerializer.get, LongSerializer.get)