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

筅森魡賤 提交于 2019-12-20 07:18:51

问题


I am using below datastax versions with java8

<dependency>
      <groupId>com.datastax.cassandra</groupId>
      <artifactId>cassandra-driver-core</artifactId>
      <version>3.7.2</version>
    </dependency>

    <dependency>
      <groupId>com.datastax.cassandra</groupId>
      <artifactId>cassandra-driver-mapping</artifactId>
      <version>3.7.2</version> 
    </dependency>

My table has a Date column as below

cass_table (                                                    
    data_source_id int,                                                                                                                                                                   
    company_id text,                                                              
    create_date date)  

When I trying to save the data into C* table as below

final IndustryCompany four = new IndustryCompany(1,1236, ProdUtils.today());
 industryCompanyRepository.save(one);


public static Date today() {
        return java.sql.Date.valueOf(new SimpleDateFormat(ProducerConstants.DATABASE_DATE_FORMAT).format(Calendar.getInstance().getTime()));
    }

Getting error :

Caused by: com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [date <-> java.sql.Date]
    at com.datastax.driver.core.exceptions.CodecNotFoundException.copy(CodecNotFoundException.java:57) ~[cassandra-driver-core-3.7.2.jar:na]

What am wrong here i am doing ? how to fix this ? any help please...


回答1:


You need to pass variable with com.datastax.driver.core.LocalDate type instead. See the documentation for mapping between Java type and CQL types. You can use LocalDate from JDK 8, or from Joda time package if you'll use so-called optional codec.

Another possibility is to write date<->java.sql.Date and register it for direct mapping.




回答2:


Thanks a lot @Alex Ott As you suggested I did below

added another jar

 <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-extras</artifactId>
        <version>3.7.2</version>
    </dependency>

Before calling save() method i called this

public void registerCodecs() {
    CodecRegistry registry = this.session.getCluster().getConfiguration().getCodecRegistry();
    registry.register(LocalDateCodec.instance);
}


来源:https://stackoverflow.com/questions/57111413/codecnotfoundexception-codec-not-found-for-requested-operation-date-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!