使用时间戳列cassandra

时间:2016-12-30 14:35:56

标签: cassandra cassandra-2.0

我想从我的表中删除时间戳之间的数据。

CREATE TABLE propatterns_test.test (
clientId text,
meterId text,
meterreading text,
date timestamp,
PRIMARY KEY (meterId, date) );

我的删除查询是:

DELETE FROM test  WHERE meterid = 'M5' AND date > '2016-12-27 10:00:00+0000';

返回了此错误:

InvalidRequest: Error from server: code=2200 [Invalid query] 
    message="Invalid operator < for PRIMARY KEY part date"

之后我试图删除一个特定的行:

DELETE FROM test  WHERE meterid = 'M5' AND date = '2016-12-27 09:42:30+0000';

实际上该表包含相同的记录,但未删除。

这就是我的数据:

 meterid | date                     | clientid | meterreading
---------+--------------------------+----------+--------------
      M5 | 2016-12-27 09:42:30+0000 |      RDS |      35417.8
      M5 | 2016-12-27 09:42:44+0000 |      RDS |      35417.8
      M5 | 2016-12-27 09:47:20+0000 |      RDS |      35417.8
      M5 | 2016-12-27 09:47:33+0000 |      RDS |      35417.8

没有从表中删除。那么如何在作为主键一部分的时间戳日期之间删除数据呢?

3 个答案:

答案 0 :(得分:3)

我看到这里发生了一些事情。首先,像iconnj所提到的,在Cassandra 3.0之前的版本中无法进行范围删除。

其次,您的单行删除尝试失败(我相信),因为您没有考虑时间戳上存在的毫秒数。如果您将date列嵌套在timestsampasblobblobasbigint函数中,则可以看到此信息:

aploetz@cqlsh:stackoverflow> SELECT meterid,date,blobAsBigint(timestampAsBlob(date)) 
    FROM propatterns WHERE meterid='M5';

 meterid | date                     | system.blobasbigint(system.timestampasblob(date))
---------+--------------------------+---------------------------------------------------
      M5 | 2016-12-27 09:42:30+0000 |                                     1482831750000
      M5 | 2016-12-30 17:31:53+0000 |                                     1483119113231
      M5 | 2016-12-30 17:32:08+0000 |                                     1483119128812

(3 rows)

注意2016-12-27 09:42:30+0000行末尾的零,我明确地从你的例子中插入了。请注意,我使用dateof(now())嵌套函数插入的两行实际上具有毫秒作为时间戳上的最后三位数。

当我删除其中一行时,观察当我取这三位数并将其添加为毫秒时会发生什么:

aploetz@cqlsh:stackoverflow> DELETE FROM propatterns WHERE meterid='M5'
    AND date='2016-12-30 17:32:08.812+0000';
aploetz@cqlsh:stackoverflow> SELECT meterid,date,blobAsBigint(timestampAsBlob(date))
    FROM propatterns WHERE meterid='M5';

 meterid | date                     | system.blobasbigint(system.timestampasblob(date))
---------+--------------------------+---------------------------------------------------
      M5 | 2016-12-27 09:42:30+0000 |                                     1482831750000
      M5 | 2016-12-30 17:31:53+0000 |                                     1483119113231

(2 rows)

总结:

  • 您无法在Cassandra 3.0之前执行范围删除。
  • 如果确实存在毫秒,则无法删除由时间戳键入的单个行而不指定毫秒。

答案 1 :(得分:1)

从C * 3.0开始,可以使用范围子句删除。看看你得到的错误,我认为你是在3.0之前的版本,在这种情况下你将无法通过CQL这样做

答案 2 :(得分:0)

在Cassandra 3中,您可以使用“ ...来自Y,使用时间戳记XXX,其中...”

create table mytime (
    location_id text,
    tour_id text,
    mytime timestamp,
PRIMARY KEY (location_id, tour_id));

INSERT INTO mytime (location_id, tour_id, mytime) values ('location1', '1', toTimeStamp(now()));
INSERT INTO mytime (location_id, tour_id, mytime) values ('location1', '2', toTimeStamp(now()));

请注意:您需要用于时间戳的值是纳秒而不是毫秒:

select location_id, mytime, blobAsBigint(mytime), WRITETIME(mytime) from mytime;

location_id |mytime                  |system.blobasbigint(mytime) |writetime(mytime) |
------------|------------------------|----------------------------|------------------|
location1   |2018-11-28-09.53.52.110 |1543395232110               |1543395232109517  |
location1   |2018-11-28-09.53.52.742 |1543395232742               |1543395232740055  |

所以现在您可以做

delete from mytime using timestamp 1543395232109517 where location_id = 'location1';

正确删除条目<= 1543395232109517:

select location_id, mytime, blobAsBigint(mytime), WRITETIME(mytime) from mytime;

location_id |mytime                  |system.blobasbigint(mytime) |writetime(mytime) |
------------|------------------------|----------------------------|------------------|
location1   |2018-11-28-09.53.52.742 |1543395232742               |1543395232740055  |