counter: 1.3.0 - (GREEN) Fixed the typing for the state-store
[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 import org.springframework.kafka.support.serializer.JsonSerde;
11
12 import java.util.Properties;
13
14
15 @Slf4j
16 public class CounterStreamProcessor
17 {
18         public static final String STORE_NAME = "counter";
19
20
21         public final KafkaStreams streams;
22
23
24         public CounterStreamProcessor(
25                         String inputTopic,
26                         String outputTopic,
27                         Properties properties,
28                         KeyValueBytesStoreSupplier storeSupplier)
29         {
30                 Topology topology = CounterStreamProcessor.buildTopology(
31                                 inputTopic,
32                                 outputTopic,
33                                 storeSupplier);
34
35                 streams = new KafkaStreams(topology, properties);
36         }
37
38         static Topology buildTopology(
39                         String inputTopic,
40                         String outputTopic,
41                         KeyValueBytesStoreSupplier storeSupplier)
42         {
43                 StreamsBuilder builder = new StreamsBuilder();
44
45                 KStream<User, Word> source = builder.stream(inputTopic);
46
47                 source
48                                 .map((key, word) -> new KeyValue<>(word, word))
49                                 .groupByKey()
50                                 .count(
51                                                 Materialized
52                                                                 .<Word, Long>as(storeSupplier)
53                                                                 .withKeySerde(new JsonSerde<>().copyWithType(Word.class).forKeys()))
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 }