如果我在列族中有两个簇键,如何在 cassandra 中进行查询
how to do the query in cassandra If i have two cluster key in column family
我有一个列族和语法如下:
CREATE TABLE sr_number_callrecord (
id int,
callerph text,
sr_number text,
callid text,
start_time text,
plan_id int,
PRIMARY KEY((sr_number), start_time, callerph)
);
我想像这样查询:
a) select * from dummy where sr_number='+919xxxx8383'
and start_time >='2014-12-02 08:23:18' limit 10;
b) select * from dummy where sr_number='+919xxxxxx83'
and start_time >='2014-12-02 08:23:18'
and callerph='+9120xxxxxxxx0' limit 10;
第一个查询工作正常,但第二个查询给出类似
的错误
Bad Request: PRIMARY KEY column "callerph" cannot be restricted
(preceding column "start_time" is either not restricted or by a non-EQ
relation)
如果我在第一个查询中得到结果,在第二个查询中我只添加一个
更多的簇键获得过滤结果,行将更少
就像您不能跳过 PRIMARY KEY 组件一样,您只能在查询的 last 组件上使用非等于运算符(这就是您的第一个查询有效的原因)。
如果您确实需要为上面列出的两个查询提供服务,则每个查询都需要单独的查询 table。要为第二个查询提供服务,如果您使用如下主键定义查询 table(具有相同的列),它将起作用:
PRIMARY KEY((sr_number), callerph, start_time)
这样您仍然按顺序指定 PRIMARY KEY 的各个部分,并且您的不等于条件是在最后一个 PRIMARY KEY 组件上。
在 where 子句中使用主键列的方式有一些限制 http://docs.datastax.com/en/cql/3.1/cql/cql_reference/select_r.html
一种适用于您的情况的解决方案是更改主键中聚簇列的顺序
CREATE TABLE sr_number_callrecord (
id int,
callerph text,
sr_number text,
callid text,
start_time text,
plan_id int,
PRIMARY KEY((sr_number), callerph, start_time,)
);
现在您可以在最后一列使用范围查询
select * from sr_number_callrecord where sr_number = '1234' and callerph = '+91123' and start_time >= '1234';
我有一个列族和语法如下:
CREATE TABLE sr_number_callrecord (
id int,
callerph text,
sr_number text,
callid text,
start_time text,
plan_id int,
PRIMARY KEY((sr_number), start_time, callerph)
);
我想像这样查询:
a) select * from dummy where sr_number='+919xxxx8383'
and start_time >='2014-12-02 08:23:18' limit 10;
b) select * from dummy where sr_number='+919xxxxxx83'
and start_time >='2014-12-02 08:23:18'
and callerph='+9120xxxxxxxx0' limit 10;
第一个查询工作正常,但第二个查询给出类似
的错误Bad Request: PRIMARY KEY column "callerph" cannot be restricted
(preceding column "start_time" is either not restricted or by a non-EQ
relation)
如果我在第一个查询中得到结果,在第二个查询中我只添加一个
更多的簇键获得过滤结果,行将更少
就像您不能跳过 PRIMARY KEY 组件一样,您只能在查询的 last 组件上使用非等于运算符(这就是您的第一个查询有效的原因)。
如果您确实需要为上面列出的两个查询提供服务,则每个查询都需要单独的查询 table。要为第二个查询提供服务,如果您使用如下主键定义查询 table(具有相同的列),它将起作用:
PRIMARY KEY((sr_number), callerph, start_time)
这样您仍然按顺序指定 PRIMARY KEY 的各个部分,并且您的不等于条件是在最后一个 PRIMARY KEY 组件上。
在 where 子句中使用主键列的方式有一些限制 http://docs.datastax.com/en/cql/3.1/cql/cql_reference/select_r.html
一种适用于您的情况的解决方案是更改主键中聚簇列的顺序
CREATE TABLE sr_number_callrecord (
id int,
callerph text,
sr_number text,
callid text,
start_time text,
plan_id int,
PRIMARY KEY((sr_number), callerph, start_time,)
);
现在您可以在最后一列使用范围查询
select * from sr_number_callrecord where sr_number = '1234' and callerph = '+91123' and start_time >= '1234';