如何在cassandra中进行查询如果我在列族中有两个簇密钥

时间:2015-08-19 10:47:11

标签: cassandra cql cassandra-2.0 pycassa

我有一个列族和语法如下:

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)  

如果我在第一个查询中得到结果,在第二个查询中我只是添加一个   获取过滤结果的群集密钥越多,行就越少

2 个答案:

答案 0 :(得分:5)

就像你不能跳过PRIMARY KEY组件一样,你只能在你查询的 last 组件上使用非等于运算符(这就是你的第一个查询有效的原因)。

如果您确实需要提供上面列出的两个查询,那么您需要为每个查询提供单独的查询表。要为第二个查询提供服务,如果使用PRIMARY KEY定义查询表(具有相同的列)将起作用:

PRIMARY KEY((sr_number), callerph, start_time)

这样你仍然按顺序指定PRIMARY KEY的部分,而你的非等于条件是在最后一个PRIMARY KEY组件上。

答案 1 :(得分:1)

在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';