0

I am using below Datastax versions with Java 8:

<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 I doing wrong here?

halfer
  • 19,824
  • 17
  • 99
  • 186
BdEngineer
  • 2,929
  • 4
  • 49
  • 85

2 Answers2

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.

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
  • thank you ... When I use version 4.0.0 instead of current version 3.7.2 to run https://github.com/lankydan/datastax-java-driver ... there are lot of errors... is there any samples/examples to use version 4.0.0 ??? – BdEngineer Jul 19 '19 at 14:28
  • 1
    4.x is major rewrite of the Java driver, and it's not binary compatible. There is a separate part of manual regarding porting: https://docs.datastax.com/en/developer/java-driver/4.1/upgrade_guide/ And examples are in repository: https://github.com/datastax/java-driver/tree/4.x/examples – Alex Ott Jul 19 '19 at 14:34
  • thank you , what do you mean by "not binary compatible" ?? – BdEngineer Jul 20 '19 at 06:37
  • 1
    That you can’t simply take your compiled code and use with new driver version, as there are API changes – Alex Ott Jul 20 '19 at 06:50
-1

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);
}
BdEngineer
  • 2,929
  • 4
  • 49
  • 85