How to use SimpleDateCodec

137 views
Skip to first unread message

Frances Jones

unread,
May 27, 2017, 12:58:49 AM5/27/17
to DataStax Java Driver for Apache Cassandra User Mailing List
I'm trying to map a Date type in Cassandra to Anything else.


Now, the code builds and there are no issues, I can read the DDL of the table through a Describe in Presto etc. However, the minute I do a select, I get the error:
Query failed (#20170525_233510_00016_tk9ph): Codec not found for requested operation: ['org.apache.cassandra.db.marshal.SimpleDateType' <-> com.datastax.driver.core.LocalDate]


Not being a Java programmer, how can I register / use this in this code? https://github.com/pawelrychlik/presto/commit/bab9c9c4a45396aa1e9cdc6bdcddb2410b46a7cd
Do I need to register it, If so, how do I do that?

Thanks

Fran



Kevin Gallardo

unread,
May 30, 2017, 6:04:26 PM5/30/17
to java-dri...@lists.datastax.com
Hi Frances,

In theory the DATE Cassandra data type should be mapping directly to the LocalDate class of the Java driver and shouldn't cause any problem the way it's done in the code sample you provided. However, I think I know why you are facing this issue. I have noticed in the presto codebase that you are for some reasons enforcing the driver to communicate to Cassandra with the Native Protocol version V3, and the protocol V3 doesn't have a Date type in its specification. So Cassandra is not able to correctly indicate the driver the data type of the column, and specifies that it is a "Custom" type, and by default the driver maps Custom types to a ByteBuffer since it doesn't have enough information to deserialize the column.

There are a couple of solutions I can think of to overcome this:
  • The simplest one is to use Protocol.V4, it should make things work out of the box without modifying your current code.
  • Other solution: you would need to deserialize the LocalDate manually with the DateCodec, as when reading the "date" column, the driver will by default expose a ByteBuffer, example there.
  • Last solution: you can register a Codec for this Custom data type, which would indicate to the driver how to deserialize this custom type when Cassandra returns it. This would require:
    1. create a Codec class that reuses the DateCodec but also indicates to the CodecRegistry that this codec should be used with the custom type "org.apache.cassandra.db.marshal.SimpleDateType". The codec should look like this.
    2. Register this codec when building your Cluster, as in this example. As I understand the structure of your project, this should be done somewhere on the clusterBuilder in this function.
    3. Then you should be able to use getDate() normally on every rows, like in the code you linked.
I'd recommend going with the first solution, otherwise if you're not ready to switch to Protocol.V4, use the last solution. However, I think the last solution is a bit complicated and probably we could figure out a simpler API to do this specific thing, I'll talk to the rest of the team about it and maybe create a Driver ticket to make this simpler. Also I think we could improve our docs around dealing with Date types in the driver.

Let me know if this helps or if you require further guidance.
Cheers.



--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.



--
Kévin Gallardo.
Software Developer in Drivers and Tools Team,
DataStax.

Kevin Gallardo

unread,
May 31, 2017, 2:38:48 PM5/31/17
to java-dri...@lists.datastax.com
Hey, 

However, I think the last solution is a bit complicated and probably we could figure out a simpler API to do this specific thing, I'll talk to the rest of the team about it and maybe create a Driver ticket to make this simpler.

As a follow-up to this, I created https://datastax-oss.atlassian.net/browse/JAVA-1483.

Cheers.
Reply all
Reply to author
Forward
0 new messages