【问题标题】:Cassandra queries without allow filtering不允许过滤的 Cassandra 查询
【发布时间】:2021-09-10 15:54:11
【问题描述】:

我是 Cassandra 的初学者,我正在尝试在没有允许过滤子句的情况下执行查询。

我创建了 2 个表:汽车零件和订单:

CREATE TABLE Autosale.parts 
(
    part_number text, 
    part_name text,
    matching_model text,
    condition text,
    description text,
    price double,

    PRIMARY KEY (part_number, matching_model, condition, part_name)
) WITH CLUSTERING ORDER BY (matching_model ASC, condition ASC,  part_name ASC);

SELECT * 
FROM parts 
WHERE matching_model ='x'; //without allow filtering

SELECT * FROM parts WHERE condition ='x';
SELECT * FROM parts WHERE part_name ='x'; 
CREATE TABLE Autohandel.zamowienia 
(
    nr_of_order int,
    id_customer uuid,
    price_total double,
    parts_name set<text>,
    collect_way text,
    if_paid boolean,
    additional_requests text,
    adress text,
    customer_name text,

    PRIMARY KEY(customer_id, customer_name, nr_of_order, if_paid)
) WITH CLUSTERING ORDER BY (customer_name ASC, nr_of_order ASC,  if_paid ASC);


SELECT * FROM parts WHERE customer_name = 'x'; 
SELECT * FROM parts WHERE nr_of_order = 'x';
SELECT * FROM parts WHERE if_paid = true;

我尝试使用 WITH CLUSTERING ORDER BY 和 MATERIALIZED VIEW,但出现其他错误,例如无法限制聚类顺序或主键列。

我会很高兴得到任何帮助。

【问题讨论】:

标签: cassandra


【解决方案1】:

您需要了解 Cassandra 如何为您检索数据。为此,您需要了解 Cassandra 如何存储数据。例如在您的以下情况

CREATE TABLE Autosale.parts (
    part_number text, 
    part_name text,
    matching_model text,
    condition text,
    description text,
    price double,
PRIMARY KEY (part_number, matching_model, condition, part_name)
)WITH CLUSTERING ORDER BY (matching_model ASC, condition ASC,  part_name ASC);

您使用PRIMARY KEY 作为(part_number, matching_model, condition, part_name) 创建了一个表格部件。现在我们尝试了解主键是如何定义的。一般主键由(Partition key, List of Clutering keys)组成。因此,在您的示例中,part_number 是分区键,matching_model, condition, part_name 是两个集群键。

现在 Cassandra 使用分区键对相同类型的行进行分组。例如,part_number 为“XYZ”的所有零件将被组合在一起。一个零件的matching_model 为“ABC”,part_name 为“Horn”,条件为“Good”。具有相同part_number 的第二部分可以将matching_model 设置为“DEF”,part_name 设置为“Mirror”,Condition 设置为“Bad”。

现在查看您的查询

Select * from parts where matching_model ='x'; //without allow filtering
Select * from parts where condition ='x';
Select * from parts where part_name ='x';

您想从parts 表中读取而不使用分区键part_number。 Cassandra 会抱怨,因为它需要分区键以优化方式响应键。它说的是这样的

Bad Request: Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING

所以我建议你学习 Cassandra 中的数据建模,了解主键,查询时分区键的重要性。您可能需要重新设计数据模型。

【讨论】:

    猜你喜欢
    • 2017-07-23
    • 2019-01-13
    • 2023-03-10
    • 2015-05-07
    • 2020-10-12
    • 2020-12-01
    • 2016-08-14
    • 2014-07-05
    • 2017-12-20
    相关资源
    最近更新 更多