cassandra 中 "IN" 条件的聚类键限制

Clustering key restriction for "IN" condition in cassandra

我在 cassandra 中有 table:

CREATE TABLE pica_pictures (
  p int,
  g text,
  id text,
  a int,
  PRIMARY KEY ((p), g, id)
)

然后我尝试 select 数据查询:

cqlsh> select * from picapica_realty.pica_pictures where p = 1 and g in ('1', '2');
Bad Request: Clustering column "g" cannot be restricted by an IN relation

我找不到此行为的原因。

这与 Cassandra 2.2 相关。

cqlsh:ks> CREATE TABLE pica_pictures (
          ...   p int,
          ...   g text,
          ...   id text,
          ...   a int,
          ...   PRIMARY KEY ((p), g, id)
          ... );
cqlsh:ks> select * from pica_pictures where p = 1 and g in ('1', '2');

 p | g | id | a
---+---+----+---

(0 rows)

正如您的 link 所描述的那样,这是有效的,因为前面的列是为相等定义的,并且 none 查询的列是集合类型。

由于您的 Cassandra 版本,这可能是一个限制。正如 Cedric 指出的那样,它在 2.2 中对他有效(或者更确切地说,没有 error-out)。

然而,当我读到你的问题时,我想起了我在 2015 年芝加哥 Cassandra 日的演讲中的一张幻灯片。来自 CQL: This is not the SQL you are looking for,第 15 段短片:

IN

  • Can only operate on the last partition key and/or the last clustering key.

当时(2015 年 4 月)Cassandra 的 most-current 版本是 2.1.4 或 2.1.5。

按照目前的情况(使用 Cassandra 2.1),您需要将主键定义调整为 PRIMARY KEY ((p), g),或者将 WHERE 子句调整为 where p = 1 and g = 1 and id in ('id1', 'id2');