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.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();
49 new JsonSerde<>(User.class),
50 new JsonSerde<>(Word.class)))
51 .map((key, word) -> new KeyValue<>(word, word))
55 .<Word, Long>as(storeSupplier)
56 .withKeySerde(new JsonSerde<>().copyWithType(Word.class).forKeys()))
58 .map((word, counter) -> new KeyValue<>(word, WordCounter.of(word, counter)))
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");