尝试在 Cassandra 中写入和读取时间戳列。注册 InstantCodec 以将 Java Instant 转换为 Cassandra 时间戳,反之亦然。
现在,当我使用以下查询插入即时信息时,它会以“2018-07-18 22:16:00.056000+0000”格式保存,而不是通常的即时信息toString() 格式“2018-07-18T22:16:00.056Z”。
insert = connector_.getSession().prepare(insertInto("keyspaceName", "tableName").value("date1", bindMarker())
因此,当我在选择查询中使用带有日期列的 CQLSH 读取行时,它会抛出
cqlsh>select * from tableName where date1 = '2018-07-18 22:16:00.056000+0000'
InvalidRequest: code=2200 [Invalid query] message="Unable to coerce '2018-07-18 22:16:00.056000+0000' to a formatted date (long)"
但是如果我使用如下所示的常用格式进行查询,那么它就可以工作。
cqlsh>select * from tableName where date1 = '2018-07-18 22:16:00.056'
date |
---------------------------------
2018-07-18 22:16:00.056000+0000
如果我可以将其保存为“2018-07-18T22:16:00.056Z”,或者能够在 CQLSH 中使用“2018-07-18”进行查询,那就更好了22:16:00.056000+0000' 在 where 子句中。
最佳答案
首先 - 您可以使用 Instant
使用的标准格式插入,例如 2018-07-18T22:16:00.056Z
- CQL supports different variants of the ISO-8601 standard 。如果添加 optional codecs,您甚至可以直接绑定(bind) Instant
值.
关于第二件事 - 根据文档:
Values for the timestamp type are encoded as 64-bit signed integers representing a number of milliseconds since the standard base time known as the epoch
但是您试图指定微秒精度,但不支持...(具有毫秒分辨率的不同变体工作得很好)。尽管我同意 CQLSH 以微秒精度输出令人困惑,并且您不能在查询中使用相同的数据...
关于java - 使用 Cassandra InstantCodec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51432653/