How to make the query to work?

时间:2016-04-21 21:54:52

标签: cassandra cassandra-2.0 cql cql3

I have Cassandra version 2.0, and in it I am totally new in it, so the question... I have table T1, with columns with names: 1,2,3...14 (for simplicity); Partitioning key is column 1 , 2; Clustering key is column 3, 1 , 5; I need to perform following query:

SELECT 1,2,7 FROM T1 where 2='A';

Column 2 is a flag, so values are repeating. I get the following error:

Unable to execute CQL query: Partitioning column 2 cannot be restricted because the preceding column 1 is either not restricted or is restricted by a non-EQ relation

So what is the right way to do it? I really need to get the data that already filtered. Thanks.

2 个答案:

答案 0 :(得分:1)

Your WHERE clause needs to include the first element of the partition key.

答案 1 :(得分:1)

因此,为了确保我理解您的架构,您已经定义了一个表T1

CREATE TABLE T1 (
  1 INT,
  2 INT,
  3 INT,
  ...
  14 INT,
  PRIMARY ((1, 2), 3, 1, 5)
);

正确?

如果是这种情况,那么Cassandra 无法找到数据来回答您的CQL查询:

SELECT 1,2,7 FROM T1 where 2 = 'A';

因为您的查询没有为列“1”提供值,否则Cassandra无法计算分区键(根据您的复合PRIMARY KEY定义,需要列“1”“2”),没有它,它无法确定在哪个环节中查看哪个节点。通过在分区键中包含“2”,您告诉Cassandra该数据必需用于确定存储位置(以及读取的位置)那个数据。

例如,根据您的架构,此查询应该工作:

SELECT 7 FROM T1 WHERE 1 = 'X' AND 2 = 'A';

因为您提供了分区键的两个值。

@Caleb Rockcliffe有很好的建议,如果上面的表定义是你工作量的一个重要部分,则需要其他的辅助/补充查找机制。您可能需要找到一些方法首先查找“1”和“2”的值,然后发出您的查询。 E.g

CREATE TABLE T1_index (
  1 INT,
  2 INT,
  PRIMARY KEY (1, 2);
);

给定“1”的值,上面将提供所有的可能“2”值,然后您可以通过它们进行迭代:

SELECT 2 FROM T1_index WHERE 1 = 'X';

然后,对于每个“1”和“2”组合,您可以然后针对表T1发出查询:

SELECT 7 FROM T1 WHERE 1 = 'X' AND 2 = 'A';

希望这有帮助!