If you want to create a state store, you need to provide a serializer and deserializer class for the type you want to use. In Kafka Stream, there is a single abstraction called Serde that wraps serializer and deserializer in a single class.
If you use .withValues(Class<K> keyClass)
it must hold that
@param keyClass the class for the keys, which must be one of the types for which Kafka has built-in serdes
Because there is no built-in Serdes
for HashMap
you need to implement one first (maybe called HashMapSerde
) and give this class to the method .withValues(Serde<K> keySerde)
. Furhtermore, you must implement the actual serializer and deserializer for HashMap
, too. If you know the generic types of your HashMap, you should specify them (what make the implementation of serializer and deserializer much simpler.
Something like this (just a sketch; generic types omitted):
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serializer;
import org.apache.kafka.common.serialization.Deserializer;
public class HashMapSerde implements Serde<HashMap> {
void configure(Map<String, ?> configs, boolean isKey) {
/* put your code here */
}
void close() {
/* put your code here */
}
Serializer<HashMap> serializer() {
return new Serializer<HashMap>() {
public void configure(Map<String, ?> configs, boolean isKey) {
/* put your code here */
}
public byte[] serialize(String topic, T data) {
/* put your code here */
}
public void close() {
/* put your code here */
}
};
}
Deserializer<HashMap> deserializer() {
return new Deserializer<HashMap>() {
public void configure(Map<String, ?> configs, boolean isKey) {
/* put your code here */
}
public T deserialize(String topic, byte[] data) {
/* put your code here */
}
public void close() {
/* put your code here */
}
};
}
}
If you want to see examples for how to implement (de)serializers and Serde
, have a look into https://github.com/apache/kafka/tree/trunk/clients/src/main/java/org/apache/kafka/common/serialization and https://github.com/apache/kafka/blob/trunk/clients/src/main/java/org/apache/kafka/common/serialization/Serdes.java