如何使用Cassandra CQL获取此表的最后50行?

时间:2017-06-03 05:43:28

标签: cassandra cql

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

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

我希望按时间戳按降序排序最后50行。

如果我尝试类似:SELECT * FROM test.comments WHERE page = 'test' AND timestamp < 1496468332,我会收到此错误:

Error from server: code=2200 [Invalid query] message="Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING"

我不想使用允许过滤,我希望查询尽可能快。

我在这里查看了另一个stackoverflow问题Cassandra cql: how to select the LAST n rows from a table并尝试了解决方案:

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

但后来我收到了这个错误:InvalidRequest: Error from server: code=2200 [Invalid query] message="Only clustering key columns can be defined in CLUSTERING ORDER directive"

我是Cassandra的新手,如果这有明显的答案,请原谅我。我似乎无法让它发挥作用。

如果有人能帮助我,我将非常感激。

2 个答案:

答案 0 :(得分:1)

而不是使用索引创建Materialized View

创建一个物化视图,其中page为分区键,msguuid为desc的聚类键顺序。

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, msguuid)
    WITH CLUSTERING ORDER BY (msguuid DESC);

虽然您使用msguuid作为当前时间戳的timeuuid,但您的数据将按时间desc排序。

要获取页面的最后50行,请使用以下查询:

SELECT * FROM comments_by_page WHERE page = 'test' LIMIT 50;

检查此链接,了解物化视图优于索引的效果以及何时不使用:http://www.datastax.com/dev/blog/materialized-view-performance-in-cassandra-3-x

答案 1 :(得分:1)

在cassandra世界中,尝试根据需要满足的查询对表进行建模。如果查询总是通过where子句“page”,而msguuid只是为了唯一性而将表重新设计为如下所示

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

现在该表自然是由msguuid订购的,并且没有任何要求物化视图的额外开销。