简体   繁体   中英

Cassandra 'timestamp' InvalidQueryException when Table created

I have a small table definition for Cassandra 3 as shown below.

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. 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/

Following the instructions, including importing com.datastax.cassandra:cassandra-driver-extras:3.1.0 I use the following code to add the Instant codec.

    // 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.

However, when I execute the cql that creates my table, as defined at the top, I get the following exception, via 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.

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
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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