Class CollectionSerde<E>
java.lang.Object
org.springframework.cloud.stream.binder.kafka.streams.serde.CollectionSerde<E>
- Type Parameters:
E- type of the underlying object that the collection holds
- All Implemented Interfaces:
Closeable,AutoCloseable,org.apache.kafka.common.serialization.Serde<Collection<E>>
public class CollectionSerde<E>
extends Object
implements org.apache.kafka.common.serialization.Serde<Collection<E>>
A convenient
Serde for Collection implementations.
Whenever a Kafka Stream application needs to collect data into a container object like
Collection, then this Serde class can be used as a convenience for
serialization needs. Some examples of where using this may handy is when the application
needs to do aggregation or reduction operations where it needs to simply hold an
Iterable type.
By default, this Serde will use JsonSerde for serializing the inner objects.
This can be changed by providing an explicit Serde during creation of this object.
Here is an example of a possible use case:
.aggregate(ArrayList::new,
(k, v, aggregates) -> {
aggregates.add(v);
return aggregates;
},
Materialized.<String, Collection<Foo>, WindowStore<Bytes, byte[]>>as(
"foo-store")
.withKeySerde(Serdes.String())
.withValueSerde(new CollectionSerde<>(Foo.class, ArrayList.class)))
*
Supported Collection types by this Serde are - ArrayList, LinkedList,
PriorityQueue and HashSet. Deserializer will throw an exception
if any other Collection types are used.- Since:
- 3.0.0
- Author:
- Soby Chacko
-
Constructor Summary
ConstructorsConstructorDescriptionCollectionSerde(Class<?> targetTypeForJsonSerde, Class<?> collectionsClass) Constructor to delegate serialization operations for the inner objects toJsonSerde.CollectionSerde(org.apache.kafka.common.serialization.Serde<E> serde, Class<?> collectionsClass) Constructor to use when the application wants to specify the type of the Serde used for the inner object. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidorg.apache.kafka.common.serialization.Deserializer<Collection<E>>org.apache.kafka.common.serialization.Serializer<Collection<E>>
-
Constructor Details
-
CollectionSerde
public CollectionSerde(org.apache.kafka.common.serialization.Serde<E> serde, Class<?> collectionsClass) Constructor to use when the application wants to specify the type of the Serde used for the inner object.- Parameters:
serde- specify an explicit SerdecollectionsClass- type of the Collection class
-
CollectionSerde
Constructor to delegate serialization operations for the inner objects toJsonSerde.- Parameters:
targetTypeForJsonSerde- target type used by the JsonSerdecollectionsClass- type of the Collection class
-
-
Method Details
-
serializer
- Specified by:
serializerin interfaceorg.apache.kafka.common.serialization.Serde<E>
-
deserializer
- Specified by:
deserializerin interfaceorg.apache.kafka.common.serialization.Serde<E>
-
configure
- Specified by:
configurein interfaceorg.apache.kafka.common.serialization.Serde<E>
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceorg.apache.kafka.common.serialization.Serde<E>
-