我正在阅读本文,其中显示了如何在cassandra中使用IN子句编写查询
https://www.datastax.com/dev/blog/a-deep-look-to-the-cql-where-clause
我创建了下表
create table foo2(id bigint, bid bigint, data set<bigint>, primary key (id, bid));
insert into foo2 (id, bid, data) values (1, 1, {1, 2});
insert into foo2 (id, bid, data) values (1, 2, {3, 4});
insert into foo2 (id, bid, data) values (1, 3, {5, 6});
Run Code Online (Sandbox Code Playgroud)
现在我写查询
select * from foo2 where id = 1 and bid IN (1, 2, 3);
Run Code Online (Sandbox Code Playgroud)
当查询选择一个集合时,不能通过按关系限制聚类列。
我搜索了此错误并找到了这个
https://issues.apache.org/jira/browse/CASSANDRA-12654
它说问题已在Cassandra 4.0中解决,但我使用了
[cqlsh 5.0.1 | Cassandra 3.10 | CQL spec 3.4.4 | Native protocol v4]
Run Code Online (Sandbox Code Playgroud)
是否有解决方法(除了有关Cassandra问题的所有答案之母change your schema)
有人指向这里:如果表具有SET类型列,则Cassandra IN查询不起作用
但这没有疑问,没有明确标记的答案。
旧答案:
使用列名代替*可以解决问题。在您的情况下,查询应该是:
select id,bid,data from foo2 where id = 1 and bid IN (1, 2, 3);
更新:我之前的答案是错误的,这应该是 Cassandra 的限制,当我们查询集合列(数据
列)
时,我们不能使用 IN 查询作为簇键。如果您确实需要 IN 查询,您可以更改表的架构:
create table foo3(id bigint, bid bigint, data set<bigint>, primary key ((id, bid)));
(表键中引入了新的括号)
在这个新模式中,分区键是id和bid列组,我们可以对它们进行 IN 查询以获取分区键。现在,下面的查询应该可以工作:
select * from foo3 where id = 1 and bid IN (1, 2, 3);
| 归档时间: |
|
| 查看次数: |
2601 次 |
| 最近记录: |