2014-08-28 67 views
1

[解決] -如何使用java/astyanax將值插入列族?

我不知道爲什麼,但刪除我的代碼

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

部分使它工作。另外,如果有人可以解釋爲什麼它這樣做,那將不勝感激。謝謝!


我一直在爲卡桑德拉cli Astyanax麻煩。我無法從Netflix Github網站中找到太多東西,所以我期待讓更多第一手經驗的人們瞭解。

我該如何將行插入已經在Cassandra中創建的Column Family?目前,我的代碼如下所示:

// 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 

創建keyspaces工作,現在我只是無法獲得列家庭改變。

+0

什麼是您的列家庭默認驗證程序? – 2014-08-28 01:23:54

+0

@SotiriosDelimanolis驗證類是UTF8Type,那你的意思是?我不太熟悉默認的驗證器是什麼。 – jj172 2014-08-28 17:03:31

+0

請不要編輯您的問題添加「已解決」或「已修復」。如果tou自己找到答案,*將其添加爲答案*。堆棧溢出允許您接受它(儘管對於聲譽較低的成員來說,等待時間較短)。 – usr2564301 2014-09-15 21:54:11

回答

2

要使用

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

(計數器),

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

默認驗證類必須設置爲CounterColumnType

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

看看this page引用this page

0

我不知道爲什麼,但刪除我的代碼

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

部分使它工作。另外,如果有人可以解釋爲什麼它這樣做,那將不勝感激。謝謝!

相關問題