如何使用java / astyanax将值插入Column Family?

时间:2014-08-28 01:12:45

标签: java cassandra astyanax

[已解决] -

我不确定原因,但删除了

m.withRow(CF_test, "acct1234")
   .incrementCounterColumn("loginCount", 1); 

我的部分代码使其成功。或者,如果有人可以解释为什么会这样做,那将是值得赞赏的。谢谢!


我为Assanax遇到过Cassandra-cli问题。我无法从Netflix Github网站上找到太多信息,因此我希望向人们提供更多的第一手经验。

我如何将行插入到我已经在Cassandra中创建的列族中?目前,我的代码如下所示:

// Inserting data
MutationBatch m = keyspace.prepareMutationBatch();


//Initialize Column family
ColumnFamily<String, String> CF_test =
new ColumnFamily<String, String>(
"users",              // Column Family Name
StringSerializer.get(),   // Key Serializer
StringSerializer.get());  // Column Serializer


m.withRow(CF_test, "default")
  .putColumn("full_name", "john", null)
  .putColumn("email", "smith", null)
  .putColumn("state", "555 Elm St", null)
  .putColumn("gender", "Male", null)
  .putColumn("birth_year", 30, null);

m.withRow(CF_test, "acct1234")
  .incrementCounterColumn("loginCount", 1);


  OperationResult<Void> result = m.execute();

但是,它给了我以下错误:

Exception in thread "main" com.netflix.astyanax.connectionpool.exceptions.BadRequestException: BadRequestException: [host=127.0.0.1(127.0.0.1):9160, latency=55(55), attempts=1]InvalidRequestException(why:invalid operation for non commutative columnfamily users)
at com.netflix.astyanax.thrift.ThriftConverter.ToConnectionPoolException(ThriftConverter.java:159)
at com.netflix.astyanax.thrift.AbstractOperationImpl.execute(AbstractOperationImpl.java:65)
at com.netflix.astyanax.thrift.AbstractOperationImpl.execute(AbstractOperationImpl.java:28)
at com.netflix.astyanax.thrift.ThriftSyncConnectionFactoryImpl$ThriftConnection.execute(ThriftSyncConnectionFactoryImpl.java:151)
at com.netflix.astyanax.connectionpool.impl.AbstractExecuteWithFailoverImpl.tryOperation(AbstractExecuteWithFailoverImpl.java:69)
at com.netflix.astyanax.connectionpool.impl.AbstractHostPartitionConnectionPool.executeWithFailover(AbstractHostPartitionConnectionPool.java:256)
at com.netflix.astyanax.thrift.ThriftKeyspaceImpl.executeOperation(ThriftKeyspaceImpl.java:485)
at com.netflix.astyanax.thrift.ThriftKeyspaceImpl.access$000(ThriftKeyspaceImpl.java:79)
at com.netflix.astyanax.thrift.ThriftKeyspaceImpl$1.execute(ThriftKeyspaceImpl.java:123)
at Test1.main(Test1.java:76)

Caused by: InvalidRequestException(why:invalid operation for non commutative columnfamily users)
at org.apache.cassandra.thrift.Cassandra$batch_mutate_result.read(Cassandra.java:20833)
at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78)
at org.apache.cassandra.thrift.Cassandra$Client.recv_batch_mutate(Cassandra.java:964)
at org.apache.cassandra.thrift.Cassandra$Client.batch_mutate(Cassandra.java:950)
at com.netflix.astyanax.thrift.ThriftKeyspaceImpl$1$1.internalExecute(ThriftKeyspaceImpl.java:129)
at com.netflix.astyanax.thrift.ThriftKeyspaceImpl$1$1.internalExecute(ThriftKeyspaceImpl.java:126)
at com.netflix.astyanax.thrift.AbstractOperationImpl.execute(AbstractOperationImpl.java:60)
... 8 more

创建密钥空间有效,现在我无法更改列系列。

2 个答案:

答案 0 :(得分:2)

使用

m.withRow(CF_test, "acct1234").incrementCounterColumn("loginCount", 1);

(柜台),

  

您必须定义一个列族(或超级列族)   列将充当计数器。

默认验证类必须设置为 CounterColumnType

  

CounterColumnType只能在default_validation_class中设置。一个   列族只包含计数器,或者根本不包含计数器。

查看引用this pagethis page

答案 1 :(得分:0)

我不确定原因,但删除了

m.withRow(CF_test, "acct1234")
   .incrementCounterColumn("loginCount", 1); 

我的部分代码使其成功。或者,如果有人可以解释为什么会这样做,那将是值得赞赏的。谢谢!