Cassandra 乐观锁 [英] Cassandra Optimistic Locking
问题描述
我有一个 cassandra 表 1:
I have a 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 ;
;
我需要在 tis 表上实现乐观锁定.当我们从 table1 中读取一行时,我们会记住它的 version_num.当我们想要更新这一行时,我们会比较当前的 version_num 值和我们记住的值.此外,我们需要在每次更新时增加 version_num.
I need to implement optimistic locking on tis table. When we read a row from table1 we remember its version_num. And when we want to update this row we compare current version_num value and value that we remembered. Also we need to increment version_num on each update.
问题:
我们不能将 version_num 放入 where 子句中,这会产生错误:Bad Request: Non PRIMARY KEY version_num found in where 子句:
We cannot put version_num into where clause, this will create an error: Bad Request: Non PRIMARY KEY version_num found in where clause:
update table where name = 'abc' and version = 3
我们不能将version_num设置为主键的一部分,因为我们需要更新它的值
We cannot set version_num as a part of primary key, because we need to update its value
您认为此问题有什么解决方案吗?
Do you see any solution for this problem?
推荐答案
在那里找到了解决方案:Cassandra 2.0,轻量级事务 http://www.datastax.com/documentation/cassandra/2.0/cassandra/dml/dml_ltwt_transaction_c.html
The solution was found there: Cassandra 2.0, Lightweight transactions 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;
我将收到带有 [applied]
列的行.此行包含布尔值:true
= 更新成功,false
= 其他情况.
I will receive a row with [applied]
column. This row contains boolean value: true
= update was successful, false
= in other case.
这篇关于Cassandra 乐观锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!