Datastax Cassandra Driver throwing CodecNotFoundException

ぐ巨炮叔叔 提交于 2019-12-01 03:49:35
Andy Tolbert

When you call bind(params...) on a PreparedStatement the driver expects you to provide values w/ java types that map to the cql types.

This error ([timestamp <-> java.lang.String]) is telling you that there is no such Codec registered that maps the java String to a cql timestamp. In the java driver, the timestamp type maps to java.util.Date. So you have 2 options here:

  1. Where the column being bound is for a timestamp, provide a Date-typed value instead of a String.
  2. Create a codec that maps timestamp <-> String. To do so you could create sub class of MappingCodec as described on the documentation site, that maps String to timestamp:
public class TimestampAsStringCodec extends MappingCodec<String, Date> {
    public TimestampAsStringCodec() { super(TypeCodec.timestamp(), String.class); }

    @Override
    protected Date serialize(String value) { ... }

    @Override
    protected String deserialize(Date value) { ... }
}

You then would need to register the Codec:

cluster.getConfiguration().getCodecRegistry()
    .register(new TimestampAsStringCodec());
NGR

Better solution is provided here

The correct mappings that the driver offers out of the box for temporal types are:

    DATE      <-> com.datastax.driver.core.LocalDate : use getDate()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!