X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fwordcount%2Fcounter%2FCounterStreamProcessor.java;h=2304e558c242359f9303d6c2fdbfe33b4b419120;hb=refs%2Fheads%2Fcounter;hp=97d460fc7a33bd4c211a00e32546642bfbfb37f8;hpb=419be8ac0668ecb0e34b3a432cf2dcca1c3642dc;p=demos%2Fkafka%2Fwordcount 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..455d895 100644 --- a/src/main/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessor.java +++ b/src/main/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessor.java @@ -19,6 +19,7 @@ import java.util.stream.Collectors; @Slf4j public class CounterStreamProcessor { + public static final String TYPE = "COUNTER"; public static final String STORE_NAME = "counter"; @@ -47,17 +48,14 @@ 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())) + .mapValues(word -> Word.of(word.getUser(), word.getWord())) .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 +85,16 @@ public class CounterStreamProcessor + public static JsonSerde inKeySerde() + { + return new JsonSerde<>(User.class); + } + + public static JsonSerde inValueSerde() + { + return new JsonSerde<>(UserWord.class); + } + public static JsonSerde outKeySerde() { return serde(true); @@ -108,17 +116,17 @@ public class CounterStreamProcessor private static String typeMappingsConfig() { - return typeMappings() - .entrySet() - .stream() - .map(entry -> entry.getKey() + ":" + entry.getValue().getName()) - .collect(Collectors.joining(",")); + return typeMappingsConfig(Word.class, WordCounter.class); } - private static Map typeMappings() + public static String typeMappingsConfig(Class keyClass, Class counterClass) { return Map.of( - "word", Word.class, - "counter", WordCounter.class); + "key", keyClass, + "counter", counterClass) + .entrySet() + .stream() + .map(entry -> entry.getKey() + ":" + entry.getValue().getName()) + .collect(Collectors.joining(",")); } }