我对cassandra非常陌生,所以听起来像是一个新手问题。
我正在运行cqlsh 5.0.1 | Cassandra 2.1.4在本地。
我有一个如下表:
CREATE TABLE master (
id uuid,
creation timestamp,
event_type text,
name text,
PRIMARY KEY(id,creation)
);
...记录如下:
id | creation | event_type | name
--------------------------------------+--------------------------+------------+------------------
305abd6d-34b8-4f36-96c6-9ea0c11be952 | 2015-04-15 14:01:54-0400 | create | test2
305abd6d-34b8-4f36-96c6-9ea0c11be952 | 2015-04-15 14:03:03-0400 | update | test2 update
7440c51c-6441-44fb-833b-6140fbe822eb | 2015-04-15 14:01:54-0400 | create | test3
7440c51c-6441-44fb-833b-6140fbe822eb | 2015-04-15 14:03:44-0400 | update | test3 update
7440c51c-6441-44fb-833b-6140fbe822eb | 2015-04-15 14:04:34-0400 | update | test3 2nd update
bf42a120-dec1-47d8-bde2-c0d76f1c93a5 | 2015-04-15 14:01:54-0400 | create | test1
如何选择具有不同ID和上次修改时间戳的所有记录。
结果应该是这样的:
305abd6d-34b8-4f36-96c6-9ea0c11be952 | 2015-04-15 14:03:03-0400 | update | test2 update
7440c51c-6441-44fb-833b-6140fbe822eb | 2015-04-15 14:04:34-0400 | update | test3 2nd update
bf42a120-dec1-47d8-bde2-c0d76f1c93a5 | 2015-04-15 14:01:54-0400 | create | test1
最佳答案
根据您当前的结构,除了使用id
查询的DISTINCT
之外,您将无法选择其他任何列。您可以使用id
作为PK创建另一个查询表,然后在该表上运行基本的SELECT
(它应始终保留上次修改日期)
CREATE TABLE querytable (
id uuid,
creation timestamp,
event_type text,
name text,
PRIMARY KEY(id)
);
SELECT * from querytable --should only contain unique ID's and the last updated creation date.
您还必须在更新母版时更新此表。
关于CQL-按ID和最新时间戳获取记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29657726/