Cassandra中的物化视图错误

时间:2018-01-25 11:09:17

标签: cassandra nosql cql cql3

我是Cassandra的新手,我正在尝试创建一个表格和物化视图。但它不起作用。

我的疑问是:

- 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...)">

1 个答案:

答案 0 :(得分:2)

Cassandra中的物化视图解决了不必维护额外的表以便通过不同的分区键进行查询的用例。但是有以下限制

  • 将物化视图中的所有基表主键用作主键。
  • (可选)将基表中的一个非PRIMARY KEY列添加到 物化视图的主要关键。
  • 不支持静态列作为PRIMARY KEY。

更多文档参考here

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

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(基本上是聚类列)排序。因此,没有强制要求添加&#34; CLUSTERING ORDER BY&#34;这里的条款。