首页 > 解决方案 > Cassandra 更新 - 带有时间戳聚类键的“位置”

问题描述

我在 cassandra 中有一张表,其结构如下:

CREATE TABLE answers (
  Id              uuid,
  Name            text,
  Description     text,
  LastVersion     boolean,
  CreationDate    timestamp,
  EditionDate     timestamp,
  PRIMARY KEY(Id, EditionDate)
)WITH CLUSTERING ORDER BY (EditionDate DESC);

问题是当我需要将LastVersion列的值更新为false. 在这种情况下,仅插入新行的值Primary Key (Id, EditionDate)+LastVersion列的值。

按此顺序:

插入:

insert into answers 
(id, name, description, lastversion, creationdate, editiondate)
values
(uuid(), 'Test 1', 'Description 1', true, dateof(now()), dateof(now()));

结果:

 id                                   | editiondate                     | creationdate                    | description   | lastversion | name
--------------------------------------+---------------------------------+---------------------------------+---------------+-------------+--------
 ac4f9ec1-8737-427c-8a63-7bdb62c93932 | 2018-08-01 19:54:51.603000+0000 | 2018-08-01 19:54:51.603000+0000 | Description 1 |        True | Test 1

更新:

update answers 
set lastversion = false 
where id = ac4f9ec1-8737-427c-8a63-7bdb62c93932 
and editiondate = '2018-08-01 19:54:51';

结果:

 id                                   | editiondate                     | creationdate                    | description   | lastversion | name
--------------------------------------+---------------------------------+---------------------------------+---------------+-------------+--------
 ac4f9ec1-8737-427c-8a63-7bdb62c93932 | 2018-08-01 19:54:51.603000+0000 | 2018-08-01 19:54:51.603000+0000 | Description 1 |        True | Test 1
 ac4f9ec1-8737-427c-8a63-7bdb62c93932 | 2018-08-01 19:54:51.000000+0000 |                            null |          null |       False |   null

怎么了?实际上,该EditionTime字段似乎有所不同,但是我在UPDATE查询上花费了相同的值。

标签: c#cassandradatastaxcqldatastax-enterprise

解决方案


您的更新使用的值editionDate与您插入的值不同,因此您的更新找不到原始行。Cassandra 的更新和插入确实是 upsert,因此正在插入带有新键的新行。

请注意,EditionDate 具有毫秒精度,但您的更新仅将其指定为最接近的秒数。


推荐阅读