如何在cassandra中为聚类列选择所有行?

时间:2017-07-01 13:51:14

标签: cassandra cassandra-3.0

我有一个Partion键:A

聚类列:B,C

我明白我可以像这样查询

Select * from table where A = ?
Select * from table where A = ? and B = ?
Select * from table where A = ? and B = ? and C = ?

在某些情况下,我希望B值为该列中的任何值。

有没有办法可以查询如下内容?

Select * from table where A = ? and B = 'any value' and C = ?

1 个答案:

答案 0 :(得分:3)

选项1:

在Cassandra中,您应该设计适合您查询的数据模型。因此,支持第四个查询(通过A和C查询,但不一定知道B值)的正确方法是创建一个新表来处理该特定查询。这个表几乎是一样的,除了CLUSTERING COLUMNS的顺序会略有不同:

PRIMARY KEY (A, C, B)

现在这个查询将起作用:

Select * from table where A = ? and C = ? 

选项2:

或者,您可以使用不同的聚类顺序创建实体化视图。现在,Cassandra将保持MV与您的表数据同步。

create materialized view mv_acbd as 
select A, B, C, D 
from TABLE1 
where A is not null and B is not null and C is not null  
primary key (A, C, B);

现在针对这个MV的查询将像魅力一样工作

Select * from mv_acbd where A = ? and C = ? 

选项3:

不是最好的,但您可以在表中使用以下查询,因为它是

Select * from table where A = ? and C = ? ALLOW FILTERING

依赖于允许过滤绝不是一个好主意,当然不是你应该在生产集群中做的事情。对于这种特殊情况,扫描位于同一分区内,性能可能会根据您的用例每个分区的聚簇列数的比例而有所不同。

相关问题