找不到所请求操作的编解码器:[date< - > java.util.Date]

时间:2017-01-05 08:40:13

标签: java cassandra

在Cassandra中,列类型设置为Date,而在Model类中,字段类型设置为带有getter和setter的java.util.Date。在com.datastax.driver.mapping.Mapper.save期间,我收到以下异常:

Codec not found for requested operation: [date <-> java.util.Date]

Caused by: com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [date <-> java.util.Date]
at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:56)
at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:25)
at com.datastax.driver.mapping.DriverThrowables.propagateCause(DriverThrowables.java:41)
at com.datastax.driver.mapping.Mapper.save(Mapper.java:272)

在Google搜索过程中找到以下内容:

DATE      <-> com.datastax.driver.core.LocalDate : use getDate()

2 个答案:

答案 0 :(得分:4)

您必须将java.util.Date转换为com.datastax.driver.core.LocalDate

示例:

 LocalDate localDate = LocalDate.fromMillisSinceEpoch(date.getTime());

答案 1 :(得分:1)

我在使用时间戳时遇到了同样的问题

reactor.core.Exceptions$ErrorCallbackNotImplemented: com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException: Codec not found for requested operation: [TIMESTAMP <-> java.util.Date]
Caused by: com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException: Codec not found for requested operation: [TIMESTAMP <-> java.util.Date]
    at com.datastax.oss.driver.internal.core.type.codec.registry.CachingCodecRegistry.createCodec(CachingCodecRegistry.java:609) ~[java-driver-core-4.9.0.jar:na]
    at com.datastax.oss.driver.internal.core.type.codec.registry.DefaultCodecRegistry$1.load(DefaultCodecRegistry.java:95) ~[java-driver-core-4.9.0.jar:na]
    at com.datastax.oss.driver.internal.core.type.codec.registry.DefaultCodecRegistry$1.load(DefaultCodecRegistry.java:92) ~[java-driver-core-4.9.0.jar:na]

从驱动程序 4.0 开始,CQL 类型时间戳不再映射到 java.util.Date,而是映射到 java.time.Instant。 使用 Instant 对我有用。