From bdd62b6b906db3b10ae04d2b4c80e6426a90ad7f Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sun, 16 Jun 2024 21:08:01 +0200 Subject: [PATCH] counter: 1.3.1 - Refined `CounterStreamProcessor` (serde-config) * Refactored the creation of the ``JsonSerde``s, that are used to consume the incomming messages. * All special ``Serdes``, that are used for incomming and outgoing messages, are created in separted methods now. * Removed unnecessary operatorx in the ``Materialized``-configuration for the state store (the operator is not necessary, because no headers are present, when deserializing from a store). --- .../counter/CounterStreamProcessor.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) 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 97d460f..c983a25 100644 --- a/src/main/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessor.java +++ b/src/main/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessor.java @@ -47,17 +47,13 @@ public class CounterStreamProcessor StreamsBuilder builder = new StreamsBuilder(); builder - .stream( - inputTopic, - Consumed.with( - new JsonSerde<>(User.class), - new JsonSerde<>(Word.class))) + .stream(inputTopic, Consumed.with(inKeySerde(), inValueSerde())) .map((key, word) -> new KeyValue<>(word, word)) .groupByKey() .count( Materialized .as(storeSupplier) - .withKeySerde(new JsonSerde<>().copyWithType(Word.class).forKeys())) + .withKeySerde(new JsonSerde<>(Word.class))) // No headers are present: fixed typing is needed! .toStream() .map((word, counter) -> new KeyValue<>(word, WordCounter.of(word, counter))) .to(outputTopic, Produced.with(outKeySerde(), outValueSerde())); @@ -87,6 +83,16 @@ public class CounterStreamProcessor + public static JsonSerde inKeySerde() + { + return new JsonSerde<>(User.class); + } + + public static JsonSerde inValueSerde() + { + return new JsonSerde<>(Word.class); + } + public static JsonSerde outKeySerde() { return serde(true); -- 2.20.1