I'm trying to use the Java Cassandra client Astyanax via Scala, but receiving this compile error:
[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] ^
Here's the code in question:
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()
}
}
What can I do in my code to get around the issue?
If it helps, you can view the Astyanax code here: https://github.com/Netflix/astyanax
from MutationBatch.java
:
<K, C> ColumnListMutation<C> withRow(ColumnFamily<K, C> columnFamily, K rowKey);
So your rowKey
has to be the same type as the first type parameter of your ColumnFamily
. Yours is a ColumnFamily[ByteBuffer, java.lang.Long]
and you've passed it "foo"
, which is a String
, not a ByteBuffer
.