InvalidRequest:code = 2200 [Invalid query] message =“无效的运算符> =对于PRIMARY KEY部分”

时间:2015-07-28 11:41:03

标签: cassandra cql cqlsh

我在cassandra中有以下数据集:

表格结构

CREATE TABLE userlog (
 term text,
 ts timestamp,
 year int,
 month int,
 day int,
 hour int,
 weekofyear int,
 dayofyear int,
 count counter,
 PRIMARY KEY (term, ts, year,month,day,hour,weekofyear,dayofyear)
);

term             | ts                       | year | month | day | hour | weekofyear | dayofyear | count
------------------+--------------------------+------+-------+-----+------+------------+-----------+-------
www.datastax.com | 2028-07-13 17:06:28+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
www.datastax.com | 2015-07-28 16:17:36+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     6
www.datastax.com | 2015-07-28 16:17:36+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
www.datastax.com | 2015-07-28 16:17:36+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
www.datastax.com | 2015-07-28 16:21:15+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
www.datastax.com | 2015-07-28 16:21:33+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
www.datastax.com | 2015-07-28 16:21:50+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
www.datastax.com | 2015-07-28 16:21:52+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
www.datastax.com | 2015-07-28 16:21:53+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
             www | 2015-07-28 16:46:00+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
            www. | 2015-07-28 16:47:00+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
            www. | 2015-07-28 16:48:00+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
            www. | 2015-07-28 16:50:00+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
            www. | 2015-07-28 16:55:00+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2

当我运行此查询时:

SELECT * FROM userlog  WHERE ts >= '2015-07-28 16:46' AND  ts <= '2015-07-28 16:55' ALLOW FILTERING;

我得到了正确的结果:

term | ts                       | year | month | day | hour | weekofyear | dayofyear | count
------+--------------------------+------+-------+-----+------+------------+-----------+-------
www  | 2015-07-28 16:46:00+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
www. | 2015-07-28 16:47:00+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
www. | 2015-07-28 16:48:00+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
www. | 2015-07-28 16:50:00+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2
www. | 2015-07-28 16:55:00+0530 | 2015 |     7 |  28 |   16 |         31 |       209 |     2

但是当我尝试删除具有相同条件的行时

DELETE FROM userlog WHERE ts >= '2015-07-28 16:46' AND  ts <= '2015-07-28 16:55';

它抛出以下错误:

InvalidRequest: code=2200 [Invalid query] message="Invalid operator >= for PRIMARY KEY part ts"

我错过了什么吗?如何删除指定时间范围内的数据?另外,有没有办法在指定的时间范围内获取数据(除了我正在做的事情?)

1 个答案:

答案 0 :(得分:3)

DELETE命令不支持范围查询或ALLOW FILTERING子句。从文档中它只支持=和IN运算符:

您可以通过完全指定分区和群集列来删除单个行。

只需指定分区键即可删除整个分区。

您可以使用IN运算符一次执行其中一些操作。

如果要有选择地从分区中删除行,可以先使用SELECT查询它们,然后在应用程序中为SELECT返回的每一行发出删除。

通常,您不希望在SELECT语句上使用ALLOW FILTERING,因为效率非常低,因此通常在执行范围查询SELECT时指定分区键。您可以构建模式,以便操作所需的信息位于已知分区中,这样您就不需要进行全表扫描来查找内容。