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.KafkaStreams;
6 import org.apache.kafka.streams.KeyValue;
7 import org.apache.kafka.streams.StreamsBuilder;
8 import org.apache.kafka.streams.Topology;
9 import org.apache.kafka.streams.kstream.Consumed;
10 import org.apache.kafka.streams.kstream.KStream;
11 import org.apache.kafka.streams.kstream.Materialized;
12 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
14 import java.util.Properties;
18 public class CounterStreamProcessor
20 public final KafkaStreams streams;
23 public CounterStreamProcessor(
26 Properties properties,
27 KeyValueBytesStoreSupplier storeSupplier)
29 Topology topology = CounterStreamProcessor.buildTopology(
34 streams = new KafkaStreams(topology, properties);
37 static Topology buildTopology(
40 KeyValueBytesStoreSupplier storeSupplier)
42 StreamsBuilder builder = new StreamsBuilder();
44 KStream<String, Word> source = builder.stream(
46 Consumed.with(Serdes.String(), null));
49 .map((key, word) -> new KeyValue<>(word, word))
51 .count(Materialized.as(storeSupplier))
53 .map((word, count) -> new KeyValue<>(word, WordCount.of(word, count)))
56 Topology topology = builder.build();
57 log.info("\n\n{}", topology.describe());
64 log.info("Starting Stream-Processor");
70 log.info("Stopping Stream-Processor");