Cassandra更新-带时间戳集群键的“哪里”

时间:2018-08-01 20:07:46

标签: c# cassandra datastax cql datastax-enterprise

我在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查询上花费了相同的值。

1 个答案:

答案 0 :(得分:2)

您的更新为editionDate使用的值与您插入的值不同,因此您的更新没有找到原始行。而且Cassandra更新和插入实际上是upsert,因此将插入具有新密钥的新行。

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

相关问题