【发布时间】:2017-03-07 16:00:07
【问题描述】:
我有一个 Cassandra 3 的小表定义,如下所示。
create table if not exists mytable(id uuid primary key, data text, timestamp lastupdated);
我希望能够将名为“lastupdated”的时间戳列映射到 java.time.Instant 类型。这可以根据 Cassandra 文档通过注册一个新的 Instant 编解码器来处理,如此处所述https://datastax.github.io/java-driver/manual/custom_codecs/extras/
按照说明,包括导入 com.datastax.cassandra:cassandra-driver-extras:3.1.0 我使用以下代码添加 Instant 编解码器。
// Extra setup to allow Cassandra Timestamp to map to Java 8 Instant
CodecRegistry myCodecRegistry;
myCodecRegistry = CodecRegistry.DEFAULT_INSTANCE;
myCodecRegistry.register(InstantCodec.instance);
Cluster cluster = Cluster.builder().
addContactPoints(extractAddresses(config())).
withCodecRegistry(myCodecRegistry).build();
当我调试这段代码时,我可以看到我的集群对象在 cluster.getConfiguration().codecRegistry 中包含新的即时编解码器,一旦它被初始化。
但是,当我执行创建表的 cql 时,如顶部所定义,我通过 session.execute 得到以下异常:
unknown type xxx.lastupdated :
com.datastax.driver.core.exceptions.InvalidQueryException: Unknown type xxx.lastupdated
我觉得我错过了一些明显的东西,因为即使没有即时编解码器也应该识别时间戳,非常感谢任何帮助。
【问题讨论】: