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