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.KStream;
6 import org.apache.kafka.streams.kstream.Materialized;
7 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
8 import org.apache.kafka.streams.state.QueryableStoreTypes;
9 import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
10 import org.springframework.kafka.support.serializer.JsonSerde;
12 import java.util.Properties;
16 public class CounterStreamProcessor
18 public static final String STORE_NAME = "counter";
21 public final KafkaStreams streams;
24 public CounterStreamProcessor(
27 Properties properties,
28 KeyValueBytesStoreSupplier storeSupplier)
30 Topology topology = CounterStreamProcessor.buildTopology(
35 streams = new KafkaStreams(topology, properties);
38 static Topology buildTopology(
41 KeyValueBytesStoreSupplier storeSupplier)
43 StreamsBuilder builder = new StreamsBuilder();
45 KStream<User, Word> source = builder.stream(inputTopic);
48 .map((key, word) -> new KeyValue<>(word, word))
52 .<Word, Long>as(storeSupplier)
53 .withKeySerde(new JsonSerde<>().copyWithType(Word.class).forKeys()))
55 .map((word, counter) -> new KeyValue<>(word, WordCounter.of(word, counter)))
58 Topology topology = builder.build();
59 log.info("\n\n{}", topology.describe());
64 ReadOnlyKeyValueStore<Word, Long> getStore()
66 return streams.store(StoreQueryParameters.fromNameAndType(STORE_NAME, QueryableStoreTypes.keyValueStore()));
71 log.info("Starting Stream-Processor");
77 log.info("Stopping Stream-Processor");