0
votes

having the below table

  "CREATE TABLE IF NOT EXISTS user_preferences (" +
                        "    user_id text," +
                        "    my_duration duration," +
                        "    last_modified timestamp," +
                        "    primary key((id))" +
                        ");";

when trying to persist the below model

import com.datastax.driver.mapping.annotations.Column;
import com.datastax.driver.mapping.annotations.PartitionKey;
import com.datastax.driver.mapping.annotations.Table;
import java.time.Duration;

@Table(name = "user_preferences")
public class UserPreferences {

    @PartitionKey
    @Column(name = "user_id")
    private String userId;

    @Column(name = "my_duration")
    private Duration myDuration;

    @Column(name = "last_modified")
    private Date lastModified;
}

i get this codec not found exception.

com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [duration <-> java.time.Duration]
    at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:57) ~[cassandra-driver-core-3.6.0.jar:na]
    at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:25) ~[cassandra-driver-core-3.6.0.jar:na]
    at com.datastax.driver.mapping.DriverThrowables.propagateCause(DriverThrowables.java:39) ~[cassandra-driver-mapping-3.6.0.jar:na]
    at com.datastax.driver.mapping.Mapper.save(Mapper.java:356) ~[cassandra-driver-mapping-3.6.0.jar:na]

NOTE: reading works fine probably because the table is not populated yet.

is java.time.Duration supported by Datastax-core 3.3.2 ?


1
Please elaborate your problem so that people can help you. - Sambit

1 Answers

1
votes

Addition from comments: Default codec returns com.datastax.driver.core.Duration! C* duration is not compatible with java.time.Duration. So you should use driver Duration type in you code or provide you own codec.

Wrong answer for this particular question but still useful if you are goint to implement your own codec, you will have to register it that way.

For other java.time classes you need to use/register extra jdk8 codecs: https://docs.datastax.com/en/developer/java-driver/3.1/manual/custom_codecs/extras/

import com.datastax.driver.extras.codecs.jdk8.InstantCodec;
import java.time.Instant;

cluster.getConfiguration().getCodecRegistry()
    .register(InstantCodec.instance);

or if you have no access to cluster object

com.datastax.driver.core.CodecRegistry.DEFAULT_INSTANCE.register(InstantCodec.instance);