【问题标题】:Cassandra - How can I compare timestamp?Cassandra - 我如何比较时间戳?
【发布时间】:2016-07-07 08:39:15
【问题描述】:

我有这张桌子:

CREATE TABLE mytablename (
  name text,
  typeid int,
  activefrom timestamp,
  status text,
  PRIMARY KEY ((name), typeid, activefrom)
)

我想查询一个有效的时间戳范围,例如这里的一些查询:

  • activefrom > '2000-01-02'
  • activefrom > '2000-01-02' AND activefrom

我注意到它不像在 sql 中那样工作。

例如

 name | typeid | activefrom               | status
 ------+--------+--------------------------+--------------
  dani |  99999 | 1970-01-26 00:00:00+0000 | OK
  dani |  99999 | 1970-01-26 01:00:00+0000 | OK
  dani |  99999 | 2070-01-26 00:00:00+0000 | OK
  dani |  99999 | 1970-01-01 00:00:00+0000 | OK

查询:

  select * from mytablename where name='dani' and typeid=99999 and activefrom > '2070-01-02 12:51:58+0000';

结果是这样的:

  dani |  99999 | 2070-01-26 00:00:00+0000 | OK
  dani |  99999 | 1970-01-01 00:00:00+0000 | OK

Cassandra 版本:

  cqlsh 4.1.1 | Cassandra 2.1.13.1218 | CQL spec 3.1.1 | Thrift protocol 19.39.0

有什么建议吗? 谢谢

【问题讨论】:

  • 你能显示你的确切查询吗?
  • 是的,当然,我只是添加了它。
  • 这听起来很奇怪。您使用的是哪个版本的 Cassandra?
  • cqlsh 4.1.1 |卡桑德拉 2.1.13.1218 | CQL 规范 3.1.1 | Thrift 协议 19.39.0
  • 这是一个想法...尝试以下查询:SELECT name, typeid, activefrom, blobasbigint(timestampasblob(activefrom)) FROM mytablename WHERE name='dani' and typeid=99999 and activefrom > '2070-01-02 12:51:58+0000'; 我很想知道您的 1970-01-01 时间戳转换为的 UNIX 时间戳。

标签: date cassandra timestamp


【解决方案1】:

我也试过了,这就是我得到的。

cqlsh:test> CREATE TABLE mytablename (
    ...   name text,
    ...   typeid int,
    ...   activefrom timestamp,
    ...   status text,
    ...   PRIMARY KEY ((name), typeid, activefrom)
    ... )
    ... ;

cqlsh:test> insert into mytablename (name,typeid,activefrom,status) values ('dani',99999,'1970-01-26 00:00:00+0000','OK');
cqlsh:test> insert into mytablename (name,typeid,activefrom,status) values ('dani',99999,'1970-01-26 01:00:00+0000','OK');
cqlsh:test> insert into mytablename (name,typeid,activefrom,status) values ('dani',99999,'2070-01-26 00:00:00+0000','OK');
cqlsh:test> insert into mytablename (name,typeid,activefrom,status) values ('dani',99999,'1970-01-01 00:00:00+0000','OK');
cqlsh:test> select * from mytablename;

 name | typeid | activefrom                      | status
------+--------+---------------------------------+--------
 dani |  99999 | 1970-01-01 00:00:00.000000+0000 |     OK
 dani |  99999 | 1970-01-26 00:00:00.000000+0000 |     OK
 dani |  99999 | 1970-01-26 01:00:00.000000+0000 |     OK
 dani |  99999 | 2070-01-26 00:00:00.000000+0000 |     OK

(4 rows)
cqlsh:test>   select * from mytablename where name='dani' and typeid=99999 and activefrom > '2070-01-02 12:51:58+0000';

 name | typeid | activefrom                      | status
------+--------+---------------------------------+--------
 dani |  99999 | 2070-01-26 00:00:00.000000+0000 |     OK

(1 rows)
cqlsh:test> 

版本信息:[cqlsh 5.0.1 | Cassandra 3.4-快照 | CQL 规范 3.4.0 |原生协议 v4]

【讨论】:

  • 所以我的问题是当我插入新行时它没有以正确的顺序插入
  • 插入订单应该不是问题。我怀疑 Cassandra 版本。你能换个版本试试吗?
  • 是的,我尝试了不同的版本,效果很好。所以它是cassandra的版本。不幸的是,我无法更改或升级它,所以我用 Java 解决了。我检索了所有数据,并在解析它们后进行了比较。不是最好的解决方案,但它有效。而且数据也不是太多,比如 10-20 每个查询最多 50 个
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-28
  • 2019-07-30
  • 2012-05-16
  • 2012-09-15
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多