在 Cassandra 中查询,从一组 UDT 中检索
Querying in Cassandra, retrieving from a SET of UDT
作为 UDT 的城市信息。城市代码将为 'work' 或 'home' 或 'templocation'。
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>>>
);
持有雇员information.It的table有一组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;
select * 来自 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中显示展开后的结果?. "Expand ON" 命令在开发中心不作为命令运行
但是,如果我复制值被复制到文本编辑器或其他编辑器中。目前这不是我主要关心的问题。
不能使用非主键查询,除非创建索引。
即使您在 part_time_employee_set
集合上创建索引,您也无法使用一块冻结字段 (firstname
) 进行查询。 Frozen fields 意味着要紧凑。
还有一件事你不能 select 集合中的部分项目。您需要 select 整个系列。
如果您想使用名字进行查询,则必须更改您的数据模型。
CREATE TABLE employee (
id int,
firstname text,
lastname text,
address_set set<frozen<city>>,
name text static,
PRIMARY KEY (id, firstname, lastname)
);
现在您可以 select 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'}}
作为 UDT 的城市信息。城市代码将为 'work' 或 'home' 或 'templocation'。
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>>>
);
持有雇员information.It的table有一组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;
select * 来自 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中显示展开后的结果?. "Expand ON" 命令在开发中心不作为命令运行 但是,如果我复制值被复制到文本编辑器或其他编辑器中。目前这不是我主要关心的问题。
不能使用非主键查询,除非创建索引。
即使您在 part_time_employee_set
集合上创建索引,您也无法使用一块冻结字段 (firstname
) 进行查询。 Frozen fields 意味着要紧凑。
还有一件事你不能 select 集合中的部分项目。您需要 select 整个系列。
如果您想使用名字进行查询,则必须更改您的数据模型。
CREATE TABLE employee (
id int,
firstname text,
lastname text,
address_set set<frozen<city>>,
name text static,
PRIMARY KEY (id, firstname, lastname)
);
现在您可以 select 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'}}