Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果列族中有两个集群键,如何在cassandra中进行查询_Cassandra_Cql_Cassandra 2.0_Pycassa - Fatal编程技术网

如果列族中有两个集群键,如何在cassandra中进行查询

如果列族中有两个集群键,如何在cassandra中进行查询,cassandra,cql,cassandra-2.0,pycassa,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) ); PRIMARY KEY((sr_number), callerph, start_time)

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

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) 
);
PRIMARY KEY((sr_number), callerph, start_time)
我希望执行如下查询:

  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)  
如果我在第一次查询中得到结果,那么在第二次查询中,我只是添加了一个

更多的集群键以获得筛选结果,行将更少

,就像您不能跳过主键组件一样,您只能在您查询的最后一个组件上使用非等于运算符(这就是第一次查询工作的原因)

如果确实需要为上面列出的两个查询提供服务,则需要为每个查询提供单独的查询表。为了服务于第二个查询,如果使用以下主键定义查询表(具有相同的列),则该查询表将起作用:

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) 
);
PRIMARY KEY((sr_number), callerph, start_time)

这样,您仍然可以按顺序指定主键的各个部分,并且非相等条件位于最后一个主键组件上。

在where子句中使用主键列的方式有一定的限制

在您的情况下,一个可行的解决方案是更改主键中集群列的顺序

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

谢谢你的信息。我正在浏览cassandra 2.2文档,了解可以执行上述查询的内容。我不确定这是否可能。我使用的是卡桑德拉2.0.11。你知道吗???@BirendraKumar 2.2使用与2.0相同的底层存储引擎,因此该查询在2.2中仍然无法工作。也许它可以在
start\u time
上有一个二级索引,但我不建议这样做。然而,3.0将具有物化视图,这将使查询表生成(并最终解决类似问题)更加容易。@BryceAtNetwork23为什么不建议使用二级索引?@Haspemulator,因为它们是为方便而设计的,而不是为了性能。@BryceAtNetwork23我相信,这样的权衡应该由用户自己进行。二级索引不是邪恶的,你只需要了解它们是如何工作的。此外,他们对您提出的解决方案也有自己的好处。