import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.Serdes;
-import org.apache.kafka.streams.KafkaStreams;
-import org.apache.kafka.streams.KeyValue;
-import org.apache.kafka.streams.StreamsBuilder;
-import org.apache.kafka.streams.StreamsConfig;
+import org.apache.kafka.streams.*;
import org.apache.kafka.streams.kstream.Consumed;
+import org.apache.kafka.streams.kstream.Grouped;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.streams.kstream.Produced;
import org.springframework.boot.SpringApplication;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
+import java.util.LinkedList;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;
.map((key, count) -> new KeyValue<>(
key.getUsername(),
Entry.newBuilder().setWord(key.getWord()).setCount(count).build()))
- .groupByKey()
+ .groupByKey(Grouped.keySerde(Serdes.String()))
.aggregate(
- () -> Ranking.newBuilder().build(),
+ () -> Ranking.newBuilder().setEntries(new LinkedList<Entry>()).build(),
(username, entry, ranking) -> {
ranking.getEntries().add(entry);
return ranking;
- },
- Materialized.with(Serdes.String(), null))
+ })
.toStream()
.to(properties.getOutputTopic(), Produced.keySerde(Serdes.String()));
props.put(AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, properties.getSchemaRegistry());
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
- streams = new KafkaStreams(builder.build(), props);
+ Topology topology = builder.build();
+ log.info("Topology:\n-----------------\n\n{}-----------------", topology.describe());
+
+ streams = new KafkaStreams(topology, props);
streams.setUncaughtExceptionHandler((Throwable e) ->
{
log.error("Unexpected error!", e);