【问题标题】:Cassandra Update - 'Where' with timestamp clustering keyCassandra 更新 - 带有时间戳聚类键的“位置”
【发布时间】:2019-01-09 12:16:15
【问题描述】:

我在 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# cassandra datastax cql datastax-enterprise


【解决方案1】:

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

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

【讨论】:

  • 是的,版本时间需要明确。但是,我需要一种方法来消除整个精度,比如强制“格式”。
猜你喜欢
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 2015-02-05
  • 2015-02-07
  • 2014-04-22
  • 2015-01-20
  • 2018-02-16
  • 1970-01-01
相关资源
最近更新 更多