I have POJO attribute as below,
@Column(name = "timeStamp")
private LocalDateTime timeStamp;
This attribute coming from a service, and need to retain the format. I am streaming input from Kafka and need to store in Cassandra.
I'm creating table using datastax API,
CreateTable createTable = SchemaBuilder.createTable("sample31", "contactNew")
.ifNotExists()
.withColumn("timeStamp", DataTypes.TIMESTAMP);
When my job execution, am facing the below exception,
Codec not found for requested operation: [timestamp <-> java.time.LocalDateTime]
I tried with different data type such as DataTypes.DURATION
, DataTypes.DATE
not working.
For Java driver 3.x you need to use a separate package - so-called "Optional codecs" by including an additional dependency:
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-extras</artifactId>
<version>driver-version</version>
</dependency>
For LocalDateTime
from Java 8 there is a LocalDateTimeCodec
(doc) that should be registered when you created a cluster object, like this:
import com.datastax.driver.extras.codecs.jdk8.LocalDateTimeCodec;
cluster.getConfiguration().getCodecRegistry()
.register(LocalDateTimeCodec.instance);
There is similar codecs for Java driver 4.x, starting with driver 4.8. Also just register necessary codec as described in documentation. But the easiest way is to register all necessary codecs at the session build time with something like this:
CqlSession session = CqlSession.builder().addTypeCodecs()....build();