WIP
[demos/kafka/wordcount] / src / main / java / de / juplo / kafka / wordcount / top10 / Top10StreamProcessor.java
index b669344..2b2cf93 100644 (file)
@@ -1,10 +1,10 @@
 package de.juplo.kafka.wordcount.top10;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.streams.KafkaStreams;
 import org.apache.kafka.streams.KeyValue;
 import org.apache.kafka.streams.StreamsBuilder;
+import org.apache.kafka.streams.Topology;
 
 import java.util.Properties;
 
@@ -18,31 +18,35 @@ public class Top10StreamProcessor
        public Top10StreamProcessor(
                        String inputTopic,
                        String outputTopic,
-                       ObjectMapper mapper,
                        Properties props)
+       {
+               Topology topology = Top10StreamProcessor.buildTopology(
+                               inputTopic,
+                               outputTopic);
+
+               streams = new KafkaStreams(topology, props);
+       }
+
+       static Topology buildTopology(
+                       String inputTopic,
+                       String outputTopic)
        {
                StreamsBuilder builder = new StreamsBuilder();
 
                builder
-                               .<Word, Counter>stream(inputTopic)
-                               .map((word, counter) ->
-                               {
-                                       Entry entry = Entry.of(word.getWord(), counter.getCounter());
-                                       return new KeyValue<>(word.getUser(), entry);
-                               })
+                               .<Key, Entry>stream(inputTopic)
+                               .map((key, entry) -> new KeyValue<>(key.getUser(), entry))
                                .groupByKey()
                                .aggregate(
                                                () -> new Ranking(),
-                                               (user, entry, ranking) ->
-                                               {
-                                                       ranking.add(entry);
-                                                       return ranking;
-                                               }
-                               )
+                                               (user, entry, ranking) ->ranking.add(entry))
                                .toStream()
                                .to(outputTopic);
 
-               streams = new KafkaStreams(builder.build(), props);
+               Topology topology = builder.build();
+               log.info("\n\n{}", topology.describe());
+
+               return topology;
        }
 
        public void start()