[英]Codec not found for requested operation: [duration <-> java.time.Duration]
有下表
"CREATE TABLE IF NOT EXISTS user_preferences (" +
" user_id text," +
" my_duration duration," +
" last_modified timestamp," +
" primary key((id))" +
");";
嘗試保留以下模型時
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;
}
我得到此編解碼器未找到異常。
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]
注意:讀取工作正常,可能是因為尚未填充表格。
Datastax-core 3.3.2支持java.time.Duration嗎?
注釋中的補充:默認編解碼器返回com.datastax.driver.core.Duration! C *持續時間與java.time.Duration不兼容。 因此,您應該在代碼中使用驅動程序Duration類型,或者提供自己的編解碼器。
這個特定問題的答案錯誤,但是如果您想實施自己的編解碼器仍然很有用,則必須以這種方式進行注冊。
對於其他java.time類,您需要使用/注冊額外的jdk8編解碼器: 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);
或者如果您無權訪問群集對象
com.datastax.driver.core.CodecRegistry.DEFAULT_INSTANCE.register(InstantCodec.instance);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.