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.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.apache.kafka.streams.kstream.Materialized;
19 import org.apache.kafka.streams.kstream.Produced;
20 import org.springframework.boot.SpringApplication;
21 import org.springframework.context.ConfigurableApplicationContext;
22 import org.springframework.stereotype.Component;
24 import javax.annotation.PostConstruct;
25 import javax.annotation.PreDestroy;
26 import java.util.Properties;
27 import java.util.concurrent.CompletableFuture;
28 import java.util.regex.Pattern;
30 import static org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse.SHUTDOWN_CLIENT;
35 public class Top10StreamProcessor
37 final static Pattern PATTERN = Pattern.compile("\\W+");
39 public final KafkaStreams streams;
42 public Top10StreamProcessor(
43 Top10ApplicationProperties properties,
45 ConfigurableApplicationContext context)
47 StreamsBuilder builder = new StreamsBuilder();
50 .<Key, Long>stream(properties.getInputTopic(), Consumed.with(null, Serdes.Long()))
51 .map((key, count) -> new KeyValue<>(
53 Entry.newBuilder().setWord(key.getWord()).setCount(count).build()))
56 () -> Ranking.newBuilder().build(),
57 (username, entry, ranking) -> {
58 ranking.getEntries().add(entry);
61 Materialized.with(Serdes.String(), null))
63 .to(properties.getOutputTopic(), Produced.keySerde(Serdes.String()));
65 Properties props = new Properties();
66 props.put(StreamsConfig.APPLICATION_ID_CONFIG, properties.getApplicationId());
67 props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, properties.getBootstrapServer());
68 props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, SpecificAvroSerde.class);
69 props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, SpecificAvroSerde.class);
70 props.put(AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, properties.getSchemaRegistry());
71 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
73 streams = new KafkaStreams(builder.build(), props);
74 streams.setUncaughtExceptionHandler((Throwable e) ->
76 log.error("Unexpected error!", e);
77 CompletableFuture.runAsync(() ->
79 log.info("Stopping application...");
80 SpringApplication.exit(context, () -> 1);
82 return SHUTDOWN_CLIENT;
89 log.info("Starting Stream-Processor");
96 log.info("Stopping Stream-Processor");