1 package de.juplo.kafka.wordcount.counter;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.streams.*;
5 import org.apache.kafka.streams.kstream.Consumed;
6 import org.apache.kafka.streams.kstream.Materialized;
7 import org.apache.kafka.streams.kstream.Produced;
8 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
9 import org.apache.kafka.streams.state.QueryableStoreTypes;
10 import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
11 import org.springframework.kafka.support.serializer.JsonSerde;
12 import org.springframework.kafka.support.serializer.JsonSerializer;
15 import java.util.Properties;
16 import java.util.stream.Collectors;
20 public class CounterStreamProcessor
22 public static final String STORE_NAME = "counter";
25 public final KafkaStreams streams;
28 public CounterStreamProcessor(
31 Properties properties,
32 KeyValueBytesStoreSupplier storeSupplier)
34 Topology topology = CounterStreamProcessor.buildTopology(
39 streams = new KafkaStreams(topology, properties);
42 static Topology buildTopology(
45 KeyValueBytesStoreSupplier storeSupplier)
47 StreamsBuilder builder = new StreamsBuilder();
50 .stream(inputTopic, Consumed.with(inKeySerde(), inValueSerde()))
51 .map((key, word) -> new KeyValue<>(word, word))
55 .<Word, Long>as(storeSupplier)
56 .withKeySerde(new JsonSerde<>(Word.class))) // No headers are present: fixed typing is needed!
58 .map((word, counter) -> new KeyValue<>(word, WordCounter.of(word, counter)))
59 .to(outputTopic, Produced.with(outKeySerde(), outValueSerde()));
61 Topology topology = builder.build();
62 log.info("\n\n{}", topology.describe());
67 ReadOnlyKeyValueStore<Word, Long> getStore()
69 return streams.store(StoreQueryParameters.fromNameAndType(STORE_NAME, QueryableStoreTypes.keyValueStore()));
74 log.info("Starting Stream-Processor");
80 log.info("Stopping Stream-Processor");
86 public static JsonSerde<User> inKeySerde()
88 return new JsonSerde<>(User.class);
91 public static JsonSerde<Word> inValueSerde()
93 return new JsonSerde<>(Word.class);
96 public static JsonSerde<Word> outKeySerde()
101 public static JsonSerde<WordCounter> outValueSerde()
106 public static <T> JsonSerde<T> serde(boolean isKey)
108 JsonSerde<T> serde = new JsonSerde<>();
110 Map.of(JsonSerializer.TYPE_MAPPINGS, typeMappingsConfig()),
115 private static String typeMappingsConfig()
117 return typeMappingsConfig(Word.class, WordCounter.class);
120 public static String typeMappingsConfig(Class wordClass, Class wordCounterClass)
124 "counter", wordCounterClass)
127 .map(entry -> entry.getKey() + ":" + entry.getValue().getName())
128 .collect(Collectors.joining(","));