我正在尝试通过 Scala 使用 Java Cassandra 客户端 Astyanax,但收到此编译错误:
[error] /massrel/metrics-batcher/src/main/scala/com/massrel/batcher/MetricsWriter.scala:35: type mismatch;
[error] found : com.netflix.astyanax.model.ColumnFamily[java.nio.ByteBuffer,java.lang.Long]
[error] required: com.netflix.astyanax.model.ColumnFamily[java.lang.Comparable[_ >: java.lang.String with java.nio.ByteBuffer <: java.lang.Comparable[_ >: java.lang.String with java.nio.ByteBuffer <: java.lang.Object]],java.lang.Long]
[error] Note: java.nio.ByteBuffer <: java.lang.Comparable[_ >: java.lang.String with java.nio.ByteBuffer <: java.lang.Comparable[_ >: java.lang.String with java.nio.ByteBuffer <: java.lang.Object]], but Java-defined class ColumnFamily is invariant in type K.
[error] You may wish to investigate a wildcard type such as `_ <: java.lang.Comparable[_ >: java.lang.String with java.nio.ByteBuffer <: java.lang.Comparable[_ >: java.lang.String with java.nio.ByteBuffer <: java.lang.Object]]`. (SLS 3.2.10)
[error] m.withRow(CF_M, "foo").incrementCounterColumn(0, 1)
[error] ^
[error] /massrel/metrics-batcher/src/main/scala/com/massrel/batcher/MetricsWriter.scala:36: type mismatch;
[error] found : com.netflix.astyanax.model.ColumnFamily[java.nio.ByteBuffer,java.lang.Long]
[error] required: com.netflix.astyanax.model.ColumnFamily[java.lang.Comparable[_ >: java.lang.String with java.nio.ByteBuffer <: java.lang.Comparable[_ >: java.lang.String with java.nio.ByteBuffer <: java.lang.Object]],java.lang.Long]
[error] Note: java.nio.ByteBuffer <: java.lang.Comparable[_ >: java.lang.String with java.nio.ByteBuffer <: java.lang.Comparable[_ >: java.lang.String with java.nio.ByteBuffer <: java.lang.Object]], but Java-defined class ColumnFamily is invariant in type K.
[error] You may wish to investigate a wildcard type such as `_ <: java.lang.Comparable[_ >: java.lang.String with java.nio.ByteBuffer <: java.lang.Comparable[_ >: java.lang.String with java.nio.ByteBuffer <: java.lang.Object]]`. (SLS 3.2.10)
[error] m.withRow(CF_M, "bar").incrementCounterColumn(0, 1)
[error] ^
这是有问题的代码:
package com.massrel.batcher
import java.nio.ByteBuffer
import scala.collection.mutable.{Map => MMap}
import com.netflix.astyanax._
import com.netflix.astyanax.impl._
import com.netflix.astyanax.connectionpool._
import com.netflix.astyanax.connectionpool.impl._
import com.netflix.astyanax.thrift._
import com.netflix.astyanax.model._
import com.netflix.astyanax.serializers._
class MetricsWriter {
val context: AstyanaxContext[Keyspace] = new AstyanaxContext.Builder()
.forCluster("cluster1")
.forKeyspace("dev1")
.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
.setConnectionPoolType(ConnectionPoolType.TOKEN_AWARE)
)
.withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("connectionpool1")
.setPort(9160)
.setMaxConnsPerHost(1)
.setSeeds("127.0.0.1:9160")
)
.withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
.buildKeyspace(ThriftFamilyFactory.getInstance());
context.start()
val keyspace = context.getEntity()
val CF_M = new ColumnFamily[ByteBuffer, java.lang.Long]("M", ByteBufferSerializer.get(), LongSerializer.get())
def writeTest() {
val m = keyspace.prepareMutationBatch()
m.withRow(CF_M, "foo").incrementCounterColumn(0, 1)
m.withRow(CF_M, "bar").incrementCounterColumn(0, 1)
m.execute()
}
}
我可以在我的代码中做些什么来解决这个问题?
如果有帮助,您可以在此处查看 Astyanax 代码:https ://github.com/Netflix/astyanax