Cassandra Order目前仅支持按照PRIMARY KEY中声明的顺序排列列

时间:2017-06-05 23:43:40

标签: cassandra

这是我用来创建表的查询:

CREATE TABLE test.comments (msguuid timeuuid, page text, userid text, username text, msg text, timestamp int, PRIMARY KEY (timestamp, msguuid));

然后我创建一个物化视图:

CREATE MATERIALIZED VIEW test.comments_by_page AS
    SELECT *
    FROM test.comments
    WHERE page IS NOT NULL AND msguuid IS NOT NULL
    PRIMARY KEY (page, timestamp, msguuid)
    WITH CLUSTERING ORDER BY (msguuid DESC);

我想按时间戳按升序排序最后50行。

这是我正在尝试的查询:

SELECT * FROM test.comments_by_page WHERE page = 'test' AND timestamp < 1496707057 ORDER BY timestamp ASC LIMIT 50;

然后会出现此错误:InvalidRequest: code=2200 [Invalid query] message="Order by currently only support the ordering of columns following their declared order in the PRIMARY KEY"

我该如何做到这一点?

2 个答案:

答案 0 :(得分:1)

物化视图规则与“标准”表格规则基本相同。如果您需要特定订单,则必须在群集密钥中指定该订单。

因此,您必须将timestamp放入群集部分。

答案 1 :(得分:0)

聚类订单语句应修改如下: //不要忘记将时间戳之前的主键放入()

CLUSTERING ORDER BY ((msguuid DESC), timestamp ASC)