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

How do I get the last 50 rows for this table using Cassandra CQL?

这是我用来创建 table:

的查询
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"

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

我在这里查看了另一个 Whosebug 问题 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 的新手,所以如果这有明显的答案,请原谅我。我似乎无法让它工作。

如果有人能帮助我,我将不胜感激。

而不是使用索引创建 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;

检查此 link 以了解物化视图对索引的性能以及何时不使用:http://www.datastax.com/dev/blog/materialized-view-performance-in-cassandra-3-x

在 cassandra 世界中,尝试根据需要满足的查询为您的 table 建模。如果查询总是通过 where 子句 "page" 并且 msguuid 只是为了唯一性而存在,请将 table 重新设计为如下所示

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));

现在 table 自然按 msguuid 排序,不需要任何额外的物化视图开销。