查询Cassandra,从一组UDT中检索

时间:2017-03-30 18:43:52

标签: cassandra cassandra-2.1 cassandra-cli

作为UDT的城市信息。城市代码将是“工作”或“家庭”或“模板定位”。

CREATE TYPE facetmanager_ps1.city (
    id int,
    citycode text,
    cityname text
);

包含part_time_employees列表的UDT;它有一套城市UDT

CREATE TYPE facetmanager_ps1.part_time_employees (
    firstname text,
    lastname text,
    address_set frozen<set<frozen<city>>>
);

保存员工信息的表。它有一组part_time_employee_set。

CREATE TABLE facetmanager_ps1.employee (
    id int PRIMARY KEY,
    name text,
    part_time_employee_set set<frozen<part_time_employees>>,
    PRIMARY KEY (id)
)


insert into employee (id,name,part_time_employee_set) 
values 
( 1, 'raghu', 
    {
        {
            firstname: 'r1',
            lastname:'r2',
            address_set : {
                {
                    id: 600000,
                    citycode: 'work',
                    cityname: 'chennai'
                },
                {
                    id:600020,
                    citycode: 'home',
                    cityname: 'kanchipuram'
                }
            }
        }
    }
);


update employee set part_time_employee_set
= part_time_employee_set + 
{ { firstname: 'arun', lastname : 'kannan', address_set :
    {
        {
            id: 600000,
            citycode:'work',
            cityname: 'chennai'
        },
        {
            id: 600000,
            citycode:'home',
            cityname: 'chennai'
        }
    }
}} where id=1;

从员工中选择*,其中id = 1; 它提供了所有part_time_employee信息

我的主要问题:

现在我只想要part_time_employee r1的员工信息。我想要他的工作地点和家庭位置,即)我想要完整的address_set 怎么弄?

select * from employee where id=1 and part_time_employee_set = { { firstname: 'r1' }};
InvalidRequest: Error from server: code=2200 [Invalid query] message="Collection column 'part_time_employee_set' (set<frozen<part_time_employees>>) cannot be restricted by a '=' relation"

select * from employee where id=1 and part_time_employee_set in { { firstname: 'r1' }};
SyntaxException: line 1:64 no viable alternative at input '{' (...employee where id=1 and [part_time_employee_set] in...)

问题2 :(小问题)

select * from employee;

<<set(2)>>

如何在devcenter中显示扩展结果? “展开ON”命令不能用作开发中心的命令 但是,如果我将值复制,则复制到textedit或其他编辑器中。在这一点上,这不是我的主要关注点。

1 个答案:

答案 0 :(得分:2)

除非您创建索引,否则无法使用非主键进行查询。

即使您在part_time_employee_set集合上创建了索引,也无法使用一段冻结字段(firstname)进行查询。冰冻的田地意味着紧凑。 另一件事是你不能选择集合中的部分项目。你需要选择整个系列。

如果要使用firstname进行查询,则必须更改数据模型。

CREATE TABLE employee (
    id int,
    firstname text,
    lastname text,
    address_set set<frozen<city>>,
    name text static,
    PRIMARY KEY (id, firstname, lastname)
);

现在,您可以选择名字为address_set

的兼职员工'r1'
SELECT * FROM employee_test WHERE id = 1 AND firstname = 'r1';

输出:

 id          | 1
 firstname   | r1
 lastname    | r2
 name        | raghu
 address_set | {{id: 600000, citycode: 'work', cityname: 'chennai'}, {id: 600020, citycode: 'home', cityname: 'kanchipuram'}}