【发布时间】:2020-11-06 19:30:09
【问题描述】:
我确信这是 Cassandra 最常见的问题。 尽管如此:
我有这个示例表:
CREATE TABLE test.test1 (
a text,
b text,
c timestamp,
id uuid,
d timestamp,
e decimal,
PRIMARY KEY ((a),c, b, id)) WITH CLUSTERING ORDER BY (b ASC, compartment ASC);
我的查询:
select b, (toUnixTimestamp(d) - toUnixTimestamp(c))/1000/60/60/24/365.25 as age from test.test1 where a = 'x' and c > -2208981600000 ;
这工作正常,但我无法获得我需要的按 b 列排序的数据。我需要 b 列中的所有条目及其对应的“年龄”。
例如:
select b, (toUnixTimestamp(d) - toUnixTimestamp(c))/1000/60/60/24/365.25 as age from test.test1 where a = 'x' and c > -2208981600000 order by b;
给出错误:
InvalidRequest: Error from server: code=2200 [Invalid query] message="Order by currently only supports the ordering of columns following their declared order in the PRIMARY KEY"
我在集群列中尝试了不同的顺序,在分区键中尝试了不同的选项,但我被一些逻辑所吸引,似乎无法智取 Cassandra 来获得我想要的东西。如果我得到我想要的排序顺序,我将失去对列“c”进行过滤的能力。
是否有一些我没有在这里应用的逻辑,或者,我必须省略什么(?)才能获得 b 列中具有相应年龄的条目列表。
【问题讨论】:
标签: sorting cassandra output primary-key