1 package de.juplo.kafka.wordcount.counter;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.common.serialization.Serdes;
5 import org.apache.kafka.streams.*;
6 import org.apache.kafka.streams.kstream.Consumed;
7 import org.apache.kafka.streams.kstream.KStream;
8 import org.apache.kafka.streams.kstream.Materialized;
9 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
10 import org.apache.kafka.streams.state.QueryableStoreTypes;
11 import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
13 import java.util.Properties;
17 public class CounterStreamProcessor
19 public static final String STORE_NAME = "counter";
22 public final KafkaStreams streams;
25 public CounterStreamProcessor(
28 Properties properties,
29 KeyValueBytesStoreSupplier storeSupplier)
31 Topology topology = CounterStreamProcessor.buildTopology(
36 streams = new KafkaStreams(topology, properties);
39 static Topology buildTopology(
42 KeyValueBytesStoreSupplier storeSupplier)
44 StreamsBuilder builder = new StreamsBuilder();
46 KStream<String, Word> source = builder.stream(
48 Consumed.with(Serdes.String(), null));
51 .map((key, word) -> new KeyValue<>(word, word))
53 .count(Materialized.as(storeSupplier))
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");