繁体   English   中英

创建表时,Cassandra'timestamp'InvalidQueryException

[英]Cassandra 'timestamp' InvalidQueryException when Table created

我有一个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对象在初始化后在cluster.getConfiguration()。codecRegistry中包含新的Instant编解码器。

但是,当我执行创建表的cql时(如顶部所定义),我通过session.execute得到以下异常:

unknown type xxx.lastupdated : 
com.datastax.driver.core.exceptions.InvalidQueryException: Unknown type xxx.lastupdated

我觉得我错过了一些显而易见的事情,因为即使没有Instant Codec,也应该识别时间戳,我们将不胜感激。

答案很简单,只需将列名称转换为类型即可!

create table if not exists mytable (
    id uuid primary key, 
    data text, 
    lastupdated timestamp
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM