简体   繁体   English

创建表时,Cassandra'timestamp'InvalidQueryException

[英]Cassandra 'timestamp' InvalidQueryException when Table created

I have a small table definition for Cassandra 3 as shown below. 我有一个Cassandra 3的小表定义,如下所示。

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

I want to be able to map the timestamp column called 'lastupdated' to the java.time.Instant type. 我希望能够将名为“ lastupdated”的时间戳列映射到java.time.Instant类型。 This can be handled according to the Cassandra documentation by registering a new Instant codec as documented here https://datastax.github.io/java-driver/manual/custom_codecs/extras/ 可以根据Cassandra文档处理此问题,方法是注册一个新的Instant编解码器,如此处https://datastax.github.io/java-driver/manual/custom_codecs/extras/所述。

Following the instructions, including importing com.datastax.cassandra:cassandra-driver-extras:3.1.0 I use the following code to add the Instant codec. 按照说明进行操作,包括导入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();

When I debug this code I can see that my Cluster object contains the new Instant codec in the cluster.getConfiguration().codecRegistry once it has been initialized. 当我调试此代码时,可以看到我的Cluster对象在初始化后在cluster.getConfiguration()。codecRegistry中包含新的Instant编解码器。

However, when I execute the cql that creates my table, as defined at the top, I get the following exception, via session.execute : 但是,当我执行创建表的cql时(如顶部所定义),我通过session.execute得到以下异常:

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

I feel that I have missed something obvious as timestamp should be recognized even without the Instant codec, any help would be much appreciated. 我觉得我错过了一些显而易见的事情,因为即使没有Instant Codec,也应该识别时间戳,我们将不胜感激。

Answer is as simple as inverting the column name with the type! 答案很简单,只需将列名称转换为类型即可!

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

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

相关问题 创建集群时,cassandra中的InvalidQueryException - InvalidQueryException in cassandra when creating cluster Cassandra InvalidQueryException: 键不能为空 - Cassandra InvalidQueryException: Key may not be empty 卡桑德拉数据库。 com.datastax.driver.core.exceptions.InvalidQueryException:未配置的表人 - Cassandra DB. com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured table person 保存java.util.UUID时,Cassandra说“InvalidQueryException:TimeUUID类型的版本无效” - Cassandra says “InvalidQueryException: Invalid version for TimeUUID type” when saving java.util.UUID 没有创建Lagom cassandra读数表 - Lagom cassandra readside table not created 如何在 cassandra 中的集合文字中使用绑定变量 - InvalidQueryException - How to use bind variable inside collection literal in cassandra - InvalidQueryException Spring Data Cassandra:InvalidQueryException:UUID 应为 16 或 0 字节 (20) - Spring Data Cassandra: InvalidQueryException: UUID should be 16 or 0 bytes (20) 何时在 Cassandra 中使用 UUID 而不是毫秒时间戳? - When to use UUID instead of millisecond timestamp in Cassandra? 卡桑德拉的时间戳 - Timestamp in Cassandra Flink 1.11.1 Cassandra 存在时间戳时 Sink 不保存 - Flink 1.11.1 Cassandra Sink does not save when a timestamp is present
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM