counter: 1.2.15 - Added assertion for the expected state
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / counter / CounterStreamProcessor.java
1 package de.juplo.kafka.wordcount.counter;
2
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;
12
13 import java.util.Properties;
14
15
16 @Slf4j
17 public class CounterStreamProcessor
18 {
19         public static final String STORE_NAME = "counter";
20
21
22         public final KafkaStreams streams;
23
24
25         public CounterStreamProcessor(
26                         String inputTopic,
27                         String outputTopic,
28                         Properties properties,
29                         KeyValueBytesStoreSupplier storeSupplier)
30         {
31                 Topology topology = CounterStreamProcessor.buildTopology(
32                                 inputTopic,
33                                 outputTopic,
34                                 storeSupplier);
35
36                 streams = new KafkaStreams(topology, properties);
37         }
38
39         static Topology buildTopology(
40                         String inputTopic,
41                         String outputTopic,
42                         KeyValueBytesStoreSupplier storeSupplier)
43         {
44                 StreamsBuilder builder = new StreamsBuilder();
45
46                 KStream<String, Word> source = builder.stream(
47                                 inputTopic,
48                                 Consumed.with(Serdes.String(), null));
49
50                 source
51                                 .map((key, word) -> new KeyValue<>(word, word))
52                                 .groupByKey()
53                                 .count(Materialized.as(storeSupplier))
54                                 .toStream()
55                                 .map((word, counter) -> new KeyValue<>(word, WordCounter.of(word, counter)))
56                                 .to(outputTopic);
57
58                 Topology topology = builder.build();
59                 log.info("\n\n{}", topology.describe());
60
61                 return topology;
62         }
63
64         ReadOnlyKeyValueStore<Word, Long> getStore()
65         {
66                 return streams.store(StoreQueryParameters.fromNameAndType(STORE_NAME, QueryableStoreTypes.keyValueStore()));
67         }
68
69         public void start()
70         {
71                 log.info("Starting Stream-Processor");
72                 streams.start();
73         }
74
75         public void stop()
76         {
77                 log.info("Stopping Stream-Processor");
78                 streams.close();
79         }
80 }