Cassandra Error: Clustering column "domain" cannot be restricted (preceding column "timestamp" is restricted by a non-EQ relation)

Cassandra Error: Clustering column "domain" cannot be restricted (preceding column "timestamp" is restricted by a non-EQ relation)

我正在玩 Cassandra(cql)。当我执行 SELECT 时,它 returns 我出错了:

Clustering column "domain" cannot be restricted (preceding column "timestamp" is restricted by a non-EQ relation).

列 "domain"(类型 varchar)和 "timestamp"(类型时间戳)是主键。

当我像这样仅按 "timestamp" 过滤时:

SELECT id, comment, language, location, section, domain, type, user, timestamp 
    FROM comments WHERE ts >= ? AND  ts <= ? ALLOW FILTERING;

工作正常,但是当我尝试过滤包括 "domain" returns 时,出现错误:

SELECT id, comment, language, location, section, domain, type, user, timestamp 
    FROM comments WHERE domain=? AND ts >= ? AND  ts <= ? ALLOW FILTERING;

创建 TABLE CQL:

CREATE TABLE table  ( 
id varchar,
type varchar,
section varchar,
comment text,
language varchar,
location text,
user int,
domain varchar,
timestamp timestamp,
PRIMARY KEY (id, timestamp, domain, user, section))
WITH CLUSTERING ORDER BY (timestamp DESC);

需要注意的重要一点是,对于 Cassandra,您需要采用以查询为中心的方法来进行数据建模。本质上,如果这是您需要支持的查询:

SELECT id, comment, language, location, section, domain, type, user, timestamp 
    FROM comments WHERE domain=? AND timestamp >= ? AND  timestamp <= ?;

那么你的 PRIMARY KEY 应该被设计成支持:

PRIMARY KEY (domain, timestamp, user, section, id))

因为您不会在 id 上查询,所以将它作为您的分区键(PRIMARY KEY 定义中最左边的键)没有意义。但您可能仍希望它成为密钥的一部分,以帮助确保唯一性。

备注:

  • 使用 ALLOW FILTERING 的查询对性能来说很糟糕,永远不要 在生产系统上使用。
  • 此解决方案假定 domainid 的基数不是无限的(可能是)。为了避免分区上未绑定行增长的可能性,您可能希望通过将 domain 与其他内容组合来 "bucket" 您的分区键。