Cassandra数据建模-按列排序

时间:2018-07-15 08:05:33

标签: cassandra

我需要一个包含两列的表格,id文字,时间时间戳记

我只想为每个id值插入一行,并查询按时间排序的X个最新行。 我该如何实现?

例如:

insert into test (id, time) values ('1', 1) if not exists; --> true
insert into test (id, time) values ('2', 2) if not exists; --> true
insert into test (id, time) values ('3', 3) if not exists; --> true
insert into test (id, time) values ('3', 4) if not exists; --> false

在这些插入语句之后,表应为:

id | time
'1'| 1
'2'| 2
'3'| 3

我希望能够运行此查询:

select * from test order by time desc limit 1; 

(并获得'3'| 3)

1 个答案:

答案 0 :(得分:1)

无法直接在Cassandra中执行此操作-您可以执行order by,但是它只能在集群列中发生,并且只能在单个分区中发生。就您而言,所有数据都位于不同的分区中。