1 package de.juplo.kafka.wordcount.top10;
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.*;
14 import org.apache.kafka.streams.kstream.Consumed;
15 import org.apache.kafka.streams.kstream.Grouped;
16 import org.apache.kafka.streams.kstream.Materialized;
17 import org.apache.kafka.streams.kstream.Produced;
18 import org.springframework.boot.SpringApplication;
19 import org.springframework.context.ConfigurableApplicationContext;
20 import org.springframework.stereotype.Component;
22 import javax.annotation.PostConstruct;
23 import javax.annotation.PreDestroy;
24 import java.util.Arrays;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Properties;
28 import java.util.concurrent.CompletableFuture;
29 import java.util.regex.Pattern;
31 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
36 public class Top10StreamProcessor
38 final static Pattern PATTERN = Pattern.compile("\\W+");
40 public final KafkaStreams streams;
43 public Top10StreamProcessor(
44 Top10ApplicationProperties properties,
46 ConfigurableApplicationContext context)
48 StreamsBuilder builder = new StreamsBuilder();
51 .<Key, Long>stream(properties.getInputTopic(), Consumed.with(null, Serdes.Long()))
52 .map((key, count) -> new KeyValue<>(
54 Entry.newBuilder().setWord(key.getWord()).setCount(count).build()))
55 .groupByKey(Grouped.keySerde(Serdes.String()))
57 () -> Ranking.newBuilder().setEntries(new LinkedList<Entry>()).build(),
58 (username, newEntry, ranking) -> {
59 List<Entry> entries = new LinkedList<>(ranking.getEntries());
61 if (entries.isEmpty())
63 entries.add(newEntry);
67 for (int i = 0; i < entries.size(); i++)
69 Entry entry = entries.get(i);
70 if (entry.getCount() <= newEntry.getCount())
72 entries.add(i, newEntry);
73 for (int j = i + 1; j < entries.size(); j++)
75 entry = entries.get(j);
76 if (entry.getWord().equals(newEntry.getWord()))
82 if (entries.size() > 10)
84 entries = entries.subList(0, 10);
90 ranking.setEntries(entries);
94 .to(properties.getOutputTopic(), Produced.keySerde(Serdes.String()));
96 Properties props = new Properties();
97 props.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
98 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
99 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, SpecificAvroSerde.class);
100 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, SpecificAvroSerde.class);
101 props.put(AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, properties.getSchemaRegistry());
102 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
104 Topology topology = builder.build();
105 log.info("Topology:\n-----------------\n\n{}-----------------", topology.describe());
107 streams = new KafkaStreams(topology, props);
108 streams.setUncaughtExceptionHandler((Throwable e) ->
110 log.error("Unexpected error!", e);
111 CompletableFuture.runAsync(() ->
113 log.info("Stopping application...");
114 SpringApplication.exit(context, () -> 1);
116 return SHUTDOWN_CLIENT;
123 log.info("Starting Stream-Processor");
130 log.info("Stopping Stream-Processor");