从 Cassandra 中的多个分区中选择,使用复合分区键?

问题描述

在 Cassandra (CQL) 中,可以查询多个分区,例如:

create table foo(i int,j int,primary key (i));
insert into foo (i,j) values (1,1);
insert into foo (i,j) values (2,2);

select * from foo where i in (1,2);

 i | j
---+---
 1 | 1
 2 | 2

但是,如果 foo 具有复合分区键,我不确定是否可行:

create table foo(i int,k int,primary key ((i,j),k));

我尝试过但被 CQL 拒绝的一些查询是:

select * from foo where (i = 1 and j = 1) or (i = 2 and j = 2);

select * from foo where (i,j) in ((1,1),(2,2));

我也试过:

select * from foo where i in (1,2) and j in (1,2);

但这太宽泛的查询,因为这也将返回值 where (i=1,j=2) or (i = 2,j=1).

解决方法

可以使用 DSE Java Driver 从客户端查询 in 子句。您必须使用 async 选项 https://docs.datastax.com/en/developer/java-driver/4.10/manual/core/async/