counter: 1.1.11 - Added a test, that is based on `TopologyTestDriver`
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / counter / CounterStreamProcessor.java
1 package de.juplo.kafka.wordcount.counter;
2
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import lombok.extern.slf4j.Slf4j;
6 import org.apache.kafka.streams.KafkaStreams;
7 import org.apache.kafka.streams.KeyValue;
8 import org.apache.kafka.streams.StreamsBuilder;
9 import org.apache.kafka.streams.Topology;
10 import org.apache.kafka.streams.kstream.KStream;
11 import org.apache.kafka.streams.kstream.Materialized;
12 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
13
14 import java.util.Properties;
15
16
17 @Slf4j
18 public class CounterStreamProcessor
19 {
20         public final KafkaStreams streams;
21
22
23         public CounterStreamProcessor(
24                         String inputTopic,
25                         String outputTopic,
26                         Properties properties,
27                         KeyValueBytesStoreSupplier storeSupplier,
28                         ObjectMapper mapper)
29         {
30                 Topology topology = CounterStreamProcessor.buildTopology(
31                                 inputTopic,
32                                 outputTopic,
33                                 storeSupplier,
34                                 mapper);
35
36                 streams = new KafkaStreams(topology, properties);
37         }
38
39         static Topology buildTopology(
40                         String inputTopic,
41                         String outputTopic,
42                         KeyValueBytesStoreSupplier storeSupplier,
43                         ObjectMapper mapper)
44         {
45                 StreamsBuilder builder = new StreamsBuilder();
46
47                 KStream<String, String> source = builder.stream(inputTopic);
48                 source
49                                 .map((username, word) ->
50                                 {
51                                         try
52                                         {
53                                                 String key = mapper.writeValueAsString(Key.of(username, word));
54                                                 return new KeyValue<>(key, word);
55                                         }
56                                         catch (JsonProcessingException e)
57                                         {
58                                                 throw new RuntimeException(e);
59                                         }
60                                 })
61                                 .groupByKey()
62                                 .count(Materialized.as(storeSupplier))
63                                 .mapValues(value->Long.toString(value))
64                                 .toStream()
65                                 .to(outputTopic);
66
67                 return builder.build();
68         }
69
70         public void start()
71         {
72                 log.info("Starting Stream-Processor");
73                 streams.start();
74         }
75
76         public void stop()
77         {
78                 log.info("Stopping Stream-Processor");
79                 streams.close();
80         }
81 }