Cassandra查询PRIMARY KEY列不能限制异常

时间:2017-08-21 04:04:53

标签: cassandra

我有一张桌子如下:

CREATE TABLE vroctest.sensor_data_3 (
    nodeid text,
    yyyymmdd int,
    hour int,
    minute int,
    second int,
    data_timestamp bigint,
    data_quality double,
    data_value blob,
    PRIMARY KEY ((nodeid, yyyymmdd), hour, minute, second, data_timestamp)

我需要在下面查询

SELECT nodeid, yyyymmdd, hour, minute, second, data_timestamp, data_quality, data_value
FROM vroctest.sensor_data_3  where nodeid in ('331ea1eb-d536-3f37-ba6b-ae02dbc736a4') and yyyymmdd in (20160701,20160702,20170701) and hour <=24 and hour >=0 and minute <61 and minute >=0 and second >=0 and second< 61 and data_timestamp >= 1467317265000 and data_timestamp <= 1498853265000;

然而,它给了我异常

com.datastax.driver.core.exceptions.InvalidQueryException: Clustering column "minute" cannot be restricted (preceding column "hour" is restricted by a non-EQ relation)

1 个答案:

答案 0 :(得分:2)

以下全部通过。前两个是教学。最后一个是完整查询的有效形式。

SELECT * FROM sensor_data_3  
    where nodeid in ('331ea1eb-d536-3f37-ba6b-ae02dbc736a4') 
    and yyyymmdd in (20160701,20160702,20170701) 
    and hour in (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24) 
    and minute <61 and minute >=0;


SELECT * FROM sensor_data_3  
  where nodeid in ('331ea1eb-d536-3f37-ba6b-ae02dbc736a4') 
  and yyyymmdd in (20160701,20160702,20170701) 
  and (hour,minute) >= (0,0) 
  and (hour,minute) < (3,15);


SELECT nodeid, yyyymmdd, hour, minute, second, data_timestamp, data_quality, data_value FROM sensor_data_3  
    where nodeid in ('331ea1eb-d536-3f37-ba6b-ae02dbc736a4') 
    and yyyymmdd in (20160701,20160702,20170701) 
    and (hour,minute,second,data_timestamp) < (24,61,61,1467317265000) 
    and (hour,minute,second,data_timestamp) >= (0,0,0,1498853265000);

小时,分钟,秒是离散的,允许多个解决方案。然而,Cassandra并不知道这一点,也不想寻找和啄食数据。它希望每个分区有一个起点和一个终点。至少这是一个经常适合我的心理模型。

Ashraful Islam提供的链接非常好。