在Cassandra中使用MATERIALIZED VIEW会出错

时间:2017-11-08 07:12:46

标签: cassandra

我有下表。

CREATE TABLE test_x (id text PRIMARY KEY, type frozen<mycustomtype>); 

mycustomtype定义如下,

CREATE TABLE mycustomtype (
   id uuid PRIMARY KEY,
   name text
 )

我已根据mycustometype提交了以下查询的物化视图。

CREATE MATERIALIZED VIEW test_x_by_mycustomtype_name AS
   SELECT id, type
   FROM test_x
   WHERE type IS NOT NULL
   PRIMARY KEY (id, type)
   WITH CLUSTERING ORDER BY (type ASC)

通过上面的视图,我希望执行以下查询。

select id from test_x_by_mycustomtype_name where type =
{id: a3e64f8f-bd44-4f28-b8d9-6938726e34d4,  name: 'Sample'};

但查询失败,说我需要使用“允许过滤”。我创建了视图,不使用ALLOW FILTERING。为什么这个错误发生在这里因为我使用了视图的主键部分?

2 个答案:

答案 0 :(得分:2)

在您看来,type列仍然是群集密钥。因此,应使用ALLOW FILTER。您可以按照以下更改视图,然后重试

 CREATE MATERIALIZED VIEW test_x_by_mycustomtype_name_2 AS
   SELECT id, type
   FROM test_x
   WHERE type IS NOT NULL
   PRIMARY KEY (type, id)
   WITH CLUSTERING ORDER BY (id ASC);
cqlsh:test> select id from test_x_by_mycustomtype_name_2 where type = {id: a3e64f8f-bd44-4f28-b8d9-6938726e34d4,  name: 'Sample'};

id
----

答案 1 :(得分:1)

更改实体化视图的主键的顺序

CREATE MATERIALIZED VIEW test_x_by_mycustomtype_name AS
   SELECT id, type
   FROM test_x
   WHERE type IS NOT NULL
   PRIMARY KEY (type, id)
   WITH CLUSTERING ORDER BY (type ASC);