What's the main difference between Codec
and Converter
s in Spring Data Cassandra?
TypeCodec
s are part of DataStax's Java Cassandra driver. Converter
's are Spring-specific to convert values on top of a driver.
They are used to serialize/deserialize values using the Cassandra protocol:
A Codec that can serialize and deserialize to and from a given CQL type and a given Java Type.
Codecs are primarily used along with data types that map natively to Java types, such as INT
to integer
and VARCHAR
to String
.
A codec can also be used to map a UDT (represented as UDTValue
) to a POJO using DataStax annotation-based Mapping add-on without requiring you to copy UDT values to POJO properties in your code.
Speaking of Spring Data for Apache Cassandra, it puts a layer of abstraction on top of the Cassandra driver. Spring Data handles object mapping and many more things that you don't get natively from the driver. Spring Data uses converters to convert a ResultSet
Row
to an object. Approaching conversion with conventions, you get a defaulted mapping behavior which does not require you to provide additional converters in the first place.
If you want to customize Spring Data's conversion, then you can provide a custom Converter
to, e.g., convert JSON (stored as String) to an object or to store data types in Cassandra that would naturally not be serializable using Spring Data's default object mapping.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments