d30e5d7aacb05ee8d00a29957335fe416ba7e2b1
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / top10 / Top10StreamProcessor.java
1 package de.juplo.kafka.wordcount.top10;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import de.juplo.kafka.wordcount.avro.Entry;
5 import de.juplo.kafka.wordcount.avro.Key;
6 import de.juplo.kafka.wordcount.avro.Ranking;
7 import io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig;
8 import io.confluent.kafka.streams.serdes.avro.SpecificAvroSerde;
9 import io.confluent.kafka.streams.serdes.avro.SpecificAvroSerializer;
10 import lombok.extern.slf4j.Slf4j;
11 import org.apache.kafka.clients.consumer.ConsumerConfig;
12 import org.apache.kafka.common.serialization.Serdes;
13 import org.apache.kafka.streams.KafkaStreams;
14 import org.apache.kafka.streams.KeyValue;
15 import org.apache.kafka.streams.StreamsBuilder;
16 import org.apache.kafka.streams.StreamsConfig;
17 import org.apache.kafka.streams.kstream.Consumed;
18 import org.springframework.boot.SpringApplication;
19 import org.springframework.context.ConfigurableApplicationContext;
20 import org.springframework.stereotype.Component;
21
22 import javax.annotation.PostConstruct;
23 import javax.annotation.PreDestroy;
24 import java.util.Properties;
25 import java.util.concurrent.CompletableFuture;
26 import java.util.regex.Pattern;
27
28 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
29
30
31 @Slf4j
32 @Component
33 public class Top10StreamProcessor
34 {
35         final static Pattern PATTERN = Pattern.compile("\\W+");
36
37         public final KafkaStreams streams;
38
39
40         public Top10StreamProcessor(
41                         Top10ApplicationProperties properties,
42                         ObjectMapper mapper,
43                         ConfigurableApplicationContext context)
44         {
45                 StreamsBuilder builder = new StreamsBuilder();
46
47                 builder
48                                 .<Key, Long>stream(properties.getInputTopic(), Consumed.with(SpecificAvroSerializer.class, Serdes.String()) )
49                                 .map((key, count) -> new KeyValue<>(
50                                                 key.getUsername(),
51                                                 Entry.newBuilder().setWord(key.getWord()).setCount(count).build()))
52                                 .groupByKey()
53                                 .aggregate(
54                                                 () -> Ranking.newBuilder().build(),
55                                                 (username, entry, ranking) -> {
56                                                         ranking.getEntries().add(entry);
57                                                         return ranking;
58                                                 })
59                                 .toStream()
60                                 .to(properties.getOutputTopic());
61
62                 Properties props = new Properties();
63                 props.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
64                 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
65                 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
66                 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, SpecificAvroSerde.class);
67                 props.put(AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, properties.getSchemaRegistry());
68                 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
69
70                 streams = new KafkaStreams(builder.build(), props);
71                 streams.setUncaughtExceptionHandler((Throwable e) ->
72                 {
73                         log.error("Unexpected error!", e);
74                         CompletableFuture.runAsync(() ->
75                         {
76                                 log.info("Stopping application...");
77                                 SpringApplication.exit(context, () -> 1);
78                         });
79                         return SHUTDOWN_CLIENT;
80                 });
81         }
82
83         @PostConstruct
84         public void start()
85         {
86                 log.info("Starting Stream-Processor");
87                 streams.start();
88         }
89
90         @PreDestroy
91         public void stop()
92         {
93                 log.info("Stopping Stream-Processor");
94                 streams.close();
95         }
96 }