Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

Ryan Quey avatar image
Ryan Quey asked Erick Ramirez edited

Is there a simple way to add a custom codec in Java driver 4.6?

I want to write a custom codec for one of my UDTs. Is there a simple way to do this with Java Driver 4.6, without having to write out an entire codec? I found this solution that seems quite simple and intuitive, but it refers to a class MappingManager that does not seem to exist in 4.6 as far as I can tell. It seems to have been done away with starting in v4.1.

driverjavacodecudtmapping
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

alexandre.dutra avatar image
alexandre.dutra answered smadhavan commented

The class MappingManager is indeed from a legacy version of the driver, don't try to use it, the example you were referring to is old.

With driver 4+, if you are using the object mapper, then UDTs are mapped for you for free, all you have to do is annotate your class with @Entity. See the mapper documentation on entities.

If you are not using the object mapper though, then you need to create a codec for your Java class. The easiest way is to extend MappingCodec, as explained here. It's usually trivial.

2 comments Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Thanks for the answer, just wanted to add that the docs for `MappingCodec` can be found here (thanks to @smadhavan for that link)

0 Likes 0 ·
smadhavan avatar image
smadhavan answered

@Ryan Quey, Custom Codecs can be implemented by following this documentation of DataStax Java Driver 4.6 series. You can also find an example implementation at this repo at DataStax-Examples.

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.