假设我有一个这样的单列表:
CREATE TABLE test (
id int,
time timestamp,
PRIMARY KEY(id)
);
现在我为每个 Json 插入数据:
INSERT INTO test JSON '{"id":1,"time":"2018-01-12T15:06:02.753Z"}'
结果是这样的:
id time
1 2018-01-12 15:06:02.753+0000
现在我再次选择条目作为 Json:
SELECT JSON * from test where id=1;
生成的 Json 看起来像这样:
{
"id":1,
"time":"2018-01-12 15:06:02.753+0000"
}
如果我现在想将上面的 json 中的字段“time”解析为 Instant,它会因为空格而失败:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2018-01-12 10:23:00.461Z' could not be parsed at index 10
我可以用“T”分隔符替换空白字符,但是有没有更好的方法来解析 Instant?
最佳答案
Datastax Java 驱动程序支持开箱即用的表到对象映射。您不必自己编写代码。
Java 驱动程序会自动将时间戳映射到 java.util.Date
对象。请参阅下面的一些相关文档:
关于java - Cassandra 数据库 : Parsing an Instant from Cassandra's Timestamp in Json fails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48229184/