2015-12-02 53 views
1

我正在使用cassandra 2.2.3並希望使用兩條語句進行批量更新。兩者都使用輕量級事務。用輕量級事務批量更新cassandra

BEGIN BATCH 
UPDATE account SET values['balance'] = 11 WHERE id = 1 IF values['balance'] = 10; 
UPDATE account SET values['balance'] = 11 WHERE id = 2 IF values['balance'] = 10; 
APPLY BATCH; 

批返回以下錯誤:

InvalidRequest: code=2200 [Invalid query] message="Batch with conditions cannot span multiple partitions". 

我明白,這是不可能做出各種的PK一批因爲分區的where子句中,但爲什麼它是不可能的在同一個PK上進行批處理?問題是IF語句,刪除它們,批處理正在工作。

那麼是否有解決方案來成功執行這樣的批量更新?或者任何解決方法?

編輯: 這是我的架構:

CREATE TABLE booking.account (
id int PRIMARY KEY, 
values map<varchar, decimal>, 
timestampCreate timestamp, 
timestampUpdate timestamp 
); 
+0

你可以顯示錶格模式嗎? – shutty

+0

我將模式添加到帖子。謝謝 –

回答

2

I understand that it is not possible to make a batch on various PKs in the where clause because of the partitions, but why it is not possible to do a batch on the same PK?

你可以做各種的PK一批在where子句中,但是不建議這樣做(請參考Cassandra: Batch loading without the Batch keyword)。

這裏的問題是條件更新(if語句)。引自datastax cql reference

In Cassandra 2.0.6 and later, you can batch conditional updates introduced as lightweight transactions in Cassandra 2.0. Only updates made to the same partition can be included in the batch because the underlying Paxos implementation works at the granularity of the partition. You can group updates that have conditions with those that do not, but when a single statement in a batch uses a condition, the entire batch is committed using a single Paxos proposal, as if all of the conditions contained in the batch apply.

那麼你真的需要批處理語句嗎?讀這個Using and misusing batches

+0

感謝您的鏈接。我使用該批次的目的是在兩個帳戶上進行相同的「預訂」,並且它們全部成功執行或不執行任何操作。我的意思是使用CAS和批次的組合將是一個很好的組合。 –

+0

@ K.E。也許你可以在應用程序級別實現交易... – bydsky

+0

是的,我認爲不會有其他解決方案。這是一個很好的兩階段提交鏈接(在mongoDB中)http://api.mongodb.org/wiki/current/two-phase%20commit.html我會試試看。謝謝 –