我有一个cassandra table1:
CREATE TABLE Policy.table1 (
name VARCHAR ,
date TIMESTAMP ,
version_num INT,
PRIMARY KEY (
name
)) WITH caching = 'all'
-- and memtable_flush_period_in_ms = 7200 ;
;
Run Code Online (Sandbox Code Playgroud)
我需要在tis表上实现乐观锁定.当我们从table1读取一行时,我们记得它的version_num.当我们想要更新这一行时,我们比较当前的version_num值和我们记住的值.我们还需要在每次更新时增加version_num.
问题:
我们不能把version_num放到where子句中,这会产生错误:错误的请求:在where子句中找到的非PRIMARY KEY version_num:
update table where name = 'abc' and version = 3
Run Code Online (Sandbox Code Playgroud)我们不能将version_num设置为主键的一部分,因为我们需要更新其值
你看到这个问题的解决方案吗?
在那里找到了解决方案:Cassandra 2.0,轻量级交易http://www.datastax.com/documentation/cassandra/2.0/cassandra/dml/dml_ltwt_transaction_c.html
如果我执行查询:
update table1 set version_num = 5 where name = 'abc' if version_num = 4;
Run Code Online (Sandbox Code Playgroud)
我将收到一行列[applied].此行包含布尔值:true= update成功,false=在其他情况下.
| 归档时间: |
|
| 查看次数: |
6210 次 |
| 最近记录: |