简体   繁体   中英

How to insert value into Column Family using java/astyanax?

[SOLVED] -

I am not sure why, but removing the

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

part of my code made it work. Alternatively, if someone could explain why it does so, that would be appreciated. Thank you!


I've been having trouble with Astyanax for Cassandra-cli. I couldn't really figure out much from the Netflix Github site, so I was looking to ask people with more firsthand experience.

How would I go about inserting rows into a Column Family that I have already created in Cassandra? Currently, my code looks like this:

// 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();

However, it gives me the following error:

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

Creating keyspaces worked, now I'm just unable to get column families changed.

To use

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

(counters),

you have to define a column family (or super column family) whose columns will act as counters.

Default validation class must be set to CounterColumnType .

CounterColumnType may only be set in the default_validation_class. A column family either contains only counters, or no counters at all.

Take a look at this page that refers to this page .

I am not sure why, but removing the

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

part of my code made it work. Alternatively, if someone could explain why it does so, that would be appreciated. Thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM