如何将datetime转换为cqlsh上的时间戳

时间:2018-05-18 17:26:57

标签: datetime cassandra timestamp cql cqlsh

我是cassandra的新手,特别是cql语法。但我有一个创建了一个时间戳的列,它让我的日期时间为'2018-05-18 03:08:58.246000 + 0000'但我在哪里过滤我创建了这个错误

InvalidRequest: Error froim server: code=2200 [Invalid query] message="Unable to coerce '2018-05-18 03:08:58.246000+0000' to formatted date (long)"

这导致我认为我需要将日期时间转换为刻度或进行某种投射。

如何在where子句中将datetime转换为cqlsh的时间戳?

1 个答案:

答案 0 :(得分:4)

虽然Cassandra使用您提到的ISO 8601标准定义的 .ffffff 格式存储时间戳分数。

cqlsh:test_keyspace> select * from timestamp_table ;

timestamp                       | other_field
---------------------------------+---------------
2018-05-18 03:08:58.246000+0000 | Other content
2018-05-18 03:08:58.000000+0000 | Other content

当与数据库交互时(即INSERT,SELECT,...),您需要使用 .fff 格式,如下所示:

cqlsh:test_keyspace> select * from timestamp_table WHERE timestamp='2018-05-18 03:08:58.123+0000';

timestamp                       | other_field
---------------------------------+---------------
2018-05-18 03:08:58.123000+0000 | Other content

否则你会收到你提到的错误。

阅读时出错

 cqlsh:test_keyspace> select * from timestamp_table WHERE timestamp='2018-05-18 03:08:58.123000+0000';
InvalidRequest: Error from server: code=2200 [Invalid query] message="Unable to coerce '2018-05-18 03:08:58.123000+0000' to a formatted date (long)"

撰写时出错

cqlsh:test_keyspace> INSERT INTO timestamp_table (timestamp , other_field ) VALUES ( '2018-05-18 03:08:58.123456+0000', 'Other content');
InvalidRequest: Error from server: code=2200 [Invalid query] message="Unable to coerce '2018-05-18 03:08:58.123456+0000' to a formatted date (long)"