Cassandra乐观锁定 [英] Cassandra Optimistic Locking

查看:441
本文介绍了Cassandra乐观锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cassandra table1:

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.

问题:


  1. p>我们不能将version_num放在where子句中,这将创建一个错误:Bad Request:Non PRIMARY KEY version_num found where where clause:
  1. 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


  • 因为我们需要更新其值

  • We cannot set version_num as a part of primary key, because we need to update its value

    这个问题?

    UPDATE。找到解决方案

    找到解决方案:
    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;
    

    我将收到一个带有[applied]列的行。此行包含布尔值:true =更新成功,false =在其他情况下。

    I will receive a row with [applied] column. This row contains boolean value: true = update was successful, false = in other case.

    推荐答案

    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其中name ='abc'if version_num = 4;
    我将收到一个带有[applied]列的行。此行包含布尔值:true =更新成功,false =在其他情况下。

    update table1 set version_num = 5 where name = 'abc' if version_num = 4; I will receive a row with [applied] column. This row contains boolean value: true = update was successful, false = in other case.

    这篇关于Cassandra乐观锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆