counter: 1.3.1 - Removed the defaults for serialization/deserialization
[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.Consumed;
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                 builder
46                                 .stream(
47                                                 inputTopic,
48                                                 Consumed.with(
49                                                                 new JsonSerde<>(User.class),
50                                                                 new JsonSerde<>(Word.class)))
51                                 .map((key, word) -> new KeyValue<>(word, word))
52                                 .groupByKey()
53                                 .count(
54                                                 Materialized
55                                                                 .<Word, Long>as(storeSupplier)
56                                                                 .withKeySerde(new JsonSerde<>().copyWithType(Word.class).forKeys()))
57                                 .toStream()
58                                 .map((word, counter) -> new KeyValue<>(word, WordCounter.of(word, counter)))
59                                 .to(outputTopic);
60
61                 Topology topology = builder.build();
62                 log.info("\n\n{}", topology.describe());
63
64                 return topology;
65         }
66
67         ReadOnlyKeyValueStore<Word, Long> getStore()
68         {
69                 return streams.store(StoreQueryParameters.fromNameAndType(STORE_NAME, QueryableStoreTypes.keyValueStore()));
70         }
71
72         public void start()
73         {
74                 log.info("Starting Stream-Processor");
75                 streams.start();
76         }
77
78         public void stop()
79         {
80                 log.info("Stopping Stream-Processor");
81                 streams.close();
82         }
83 }