在卡桑德拉物化视图错误(Materialised view error in Cassandra)

2019-10-31 06:26发布

我是新来卡桑德拉,我想创建一个表和物化视图。 但它不工作。

我的查询是:

- all_orders

create table all_orders (
    id uuid,
    order_number bigint,
    country text,
    store_number bigint,
    supplier_number bigint,
    flow_type int,
    planned_delivery_date timestamp,
    locked boolean,
    primary key ( order_number,store_number,supplier_number,planned_delivery_date ));

- orders_by_date

CREATE MATERIALIZED VIEW orders_by_date AS 
    SELECT 
        id,
        order_number,
        country,
        store_number,
        supplier_number,
        flow_type,
        planned_delivery_date,
        locked,
    FROM all_orders
    WHERE planned_delivery_date IS NOT NULL AND order_number IS NOT NULL 
    PRIMARY KEY ( planned_delivery_date )
    WITH CLUSTERING ORDER BY (store_number,supplier_number);

我得到这样一个例外:

SyntaxException: <ErrorMessage code=2000 [Syntax error in CQL query]
message="line 1:7 no viable alternative at input 'MATERIALIZED' ([CREATE] MATERI
ALIZED...)">

Answer 1:

在卡桑德拉物化视图解决不必维持附加表(一个或多个),用于通过不同的分区键查询的使用情况。 但带有以下限制

  • 使用所有基表主键在物化视图中作为主键。
  • 任选地,从基表中添加一个非主键列到物化视图的主键。
  • 静态列不支持作为主键。

更多的文档参考这里 。

因此,在您添加物化视图的情况下,正确的语法是

CREATE MATERIALIZED VIEW orders_by_date AS 
    SELECT  id, 
            order_number, 
            country, 
            store_number, 
            supplier_number, 
            flow_type, 
            planned_delivery_date, 
            locked
            FROM all_orders
            WHERE planned_delivery_date IS NOT NULL AND order_number IS NOT NULL AND store_number IS NOT NULL AND supplier_number IS NOT NULL
            PRIMARY KEY ( planned_delivery_date, store_number, supplier_number, order_number );

这里planned_delivery_date是分区键和行由store_number,supplier_number,ORDER_NUMBER(基本聚类列)排序。 所以没有强制性要求加入“集群ORDER BY”的条款在这里。



文章来源: Materialised view error in Cassandra