From: Kai Moritz Date: Sun, 16 Jun 2024 17:57:23 +0000 (+0200) Subject: counter: 1.3.1 - Refined/Simplified the type-mapping X-Git-Tag: counter-1.3.1~2 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=419be8ac0668ecb0e34b3a432cf2dcca1c3642dc;p=demos%2Fkafka%2Fwordcount counter: 1.3.1 - Refined/Simplified the type-mapping * Removed all explicit type-mappings for the internally used types. * This greatly simplifies the configuration, because it is sufficient to configure the trusted package to serialize/deserialize all internally used types. * To make this possible, the type-mappings for the outgoing messages are specified with `Produced.with()` in the ``to()``-operation. --- diff --git a/src/main/java/de/juplo/kafka/wordcount/counter/CounterApplicationConfiguriation.java b/src/main/java/de/juplo/kafka/wordcount/counter/CounterApplicationConfiguriation.java index d9579a5..174521f 100644 --- a/src/main/java/de/juplo/kafka/wordcount/counter/CounterApplicationConfiguriation.java +++ b/src/main/java/de/juplo/kafka/wordcount/counter/CounterApplicationConfiguriation.java @@ -50,11 +50,7 @@ public class CounterApplicationConfiguriation propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName()); propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName()); - propertyMap.put( - JsonDeserializer.TYPE_MAPPINGS, - "user:" + User.class.getName() + "," + - "word:" + Word.class.getName() + "," + - "counter:" + WordCounter.class.getName()); + propertyMap.put(JsonDeserializer.TRUSTED_PACKAGES, CounterApplication.class.getPackageName()); return propertyMap; } diff --git a/src/main/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessor.java b/src/main/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessor.java index 8b9c12b..97d460f 100644 --- a/src/main/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessor.java +++ b/src/main/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessor.java @@ -4,12 +4,16 @@ import lombok.extern.slf4j.Slf4j; import org.apache.kafka.streams.*; import org.apache.kafka.streams.kstream.Consumed; import org.apache.kafka.streams.kstream.Materialized; +import org.apache.kafka.streams.kstream.Produced; import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier; import org.apache.kafka.streams.state.QueryableStoreTypes; import org.apache.kafka.streams.state.ReadOnlyKeyValueStore; import org.springframework.kafka.support.serializer.JsonSerde; +import org.springframework.kafka.support.serializer.JsonSerializer; +import java.util.Map; import java.util.Properties; +import java.util.stream.Collectors; @Slf4j @@ -56,7 +60,7 @@ public class CounterStreamProcessor .withKeySerde(new JsonSerde<>().copyWithType(Word.class).forKeys())) .toStream() .map((word, counter) -> new KeyValue<>(word, WordCounter.of(word, counter))) - .to(outputTopic); + .to(outputTopic, Produced.with(outKeySerde(), outValueSerde())); Topology topology = builder.build(); log.info("\n\n{}", topology.describe()); @@ -80,4 +84,41 @@ public class CounterStreamProcessor log.info("Stopping Stream-Processor"); streams.close(); } + + + + public static JsonSerde outKeySerde() + { + return serde(true); + } + + public static JsonSerde outValueSerde() + { + return serde(false); + } + + public static JsonSerde serde(boolean isKey) + { + JsonSerde serde = new JsonSerde<>(); + serde.configure( + Map.of(JsonSerializer.TYPE_MAPPINGS, typeMappingsConfig()), + isKey); + return serde; + } + + private static String typeMappingsConfig() + { + return typeMappings() + .entrySet() + .stream() + .map(entry -> entry.getKey() + ":" + entry.getValue().getName()) + .collect(Collectors.joining(",")); + } + + private static Map typeMappings() + { + return Map.of( + "word", Word.class, + "counter", WordCounter.class); + } }