2

Following is the sample definition of the Cassandra table I have; CREATE TABLE IF NOT EXISTS test_table ( id int, data_date date, score double, PRIMARY KEY (id) );

I have created a TestTable Class which extends Serializable and the member dataDate data type has been defined as com.datastax.driver.core.LocalDate.

However when I try writing this data using CassandraIO.write I face the error Caused by: java.io.NotSerializableException: com.datastax.driver.core.LocalDate

I Understand this error is because com.datastax.driver.core.LocalDate doesn't implement Serializable interface

Hence I tried changing the data type of the member dataDate to java.time.LocalDate However this change results in the error

java.lang.RuntimeException:org.apache.beam.sdk.util.UserCodeException:java.util.concurrent.ExecutionException:
com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [date <-> java.time.LocalDate]

So the question is; if I have a column of datatype date in Cassandra, how do I insert it using Apache Beam's CassandraIO

Yamini
  • 129
  • 1
  • 10

1 Answers1

2

Java driver can work with LocalDate from Java 8 only if you add the jar with optional codecs, and register them - but it's not clear if you can intercept establishing of the connection to Cassandra in Beam job.

You need to add following dependency to pom.xml (adjust version to match version of driver in Beam):

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

And register codec somewhere:

import com.datastax.driver.extras.codecs.jdk8.LocalDateCodec;
import java.time.Instant;

cluster.getConfiguration().getCodecRegistry()
    .register(LocalDateCodec.instance);

Looking into Beam, it looks like that it uses Achiles that supports @Codec annotation, and already includes optional codecs, so you can annotate your field with @Codec(com.datastax.driver.extras.codecs.jdk8.InstantCodec.class).

If it doesn't support Achiles, but instead uses Object Mapper from Java driver, then you can use following annotation: @Column(codec = com.datastax.driver.extras.codecs.jdk8.InstantCodec.class)

Alex Ott
  • 80,552
  • 8
  • 87
  • 132