通过Scala使用Astyanax(Cassandra客户端)时输入不匹配

时间:2012-10-08 21:56:34

标签: scala types hector astyanax

我正在尝试通过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

1 个答案:

答案 0 :(得分:1)

来自MutationBatch.java

<K, C> ColumnListMutation<C> withRow(ColumnFamily<K, C> columnFamily, K rowKey);

因此,您的rowKey必须与ColumnFamily的第一个类型参数的类型相同。你的ColumnFamily[ByteBuffer, java.lang.Long]"foo",你已经传递了String,这是ByteBuffer,而不是{{1}}。