Die Key-Statistiken werden in einer MongoDB gespeichert
[demos/kafka/training] / src / main / java / de / juplo / kafka / EndlessConsumer.java
index 9aa8152..e67bf41 100644 (file)
@@ -22,6 +22,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
 public class EndlessConsumer implements Runnable
 {
   private final ExecutorService executor;
+  private final PartitionStatisticsRepository repository;
   private final String bootstrapServer;
   private final String groupId;
   private final String id;
@@ -33,11 +34,12 @@ public class EndlessConsumer implements Runnable
   private KafkaConsumer<String, String> consumer = null;
   private Future<?> future = null;
 
-  private final Map<Integer, Map<String, Integer>> seen = new HashMap<>();
+  private final Map<TopicPartition, PartitionStatistics> seen = new HashMap<>();
 
 
   public EndlessConsumer(
       ExecutorService executor,
+      PartitionStatisticsRepository repository,
       String bootstrapServer,
       String groupId,
       String clientId,
@@ -45,6 +47,7 @@ public class EndlessConsumer implements Runnable
       String autoOffsetReset)
   {
     this.executor = executor;
+    this.repository = repository;
     this.bootstrapServer = bootstrapServer;
     this.groupId = groupId;
     this.id = clientId;
@@ -74,13 +77,36 @@ public class EndlessConsumer implements Runnable
         @Override
         public void onPartitionsRevoked(Collection<TopicPartition> partitions)
         {
-          partitions.forEach(tp -> seen.remove(tp.partition()));
+          partitions.forEach(tp ->
+          {
+            log.info("{} - removing partition: {}", id, tp);
+            PartitionStatistics removed = seen.remove(tp);
+            for (KeyCounter counter : removed.getStatistics())
+            {
+              log.info(
+                  "{} - Seen {} messages for partition={}|key={}",
+                  id,
+                  counter.getResult(),
+                  removed.getPartition(),
+                  counter.getKey());
+            }
+            repository.save(new StatisticsDocument(removed));
+          });
         }
 
         @Override
         public void onPartitionsAssigned(Collection<TopicPartition> partitions)
         {
-          partitions.forEach(tp -> seen.put(tp.partition(), new HashMap<>()));
+          partitions.forEach(tp ->
+          {
+            log.info("{} - adding partition: {}", id, tp);
+            seen.put(
+                tp,
+                repository
+                    .findById(tp.toString())
+                    .map(PartitionStatistics::new)
+                    .orElse(new PartitionStatistics(tp)));
+          });
         }
       });
 
@@ -104,16 +130,9 @@ public class EndlessConsumer implements Runnable
               record.value()
           );
 
-          Integer partition = record.partition();
+          TopicPartition partition = new TopicPartition(record.topic(), record.partition());
           String key = record.key() == null ? "NULL" : record.key();
-          Map<String, Integer> byKey = seen.get(partition);
-
-          if (!byKey.containsKey(key))
-            byKey.put(key, 0);
-
-          int seenByKey = byKey.get(key);
-          seenByKey++;
-          byKey.put(key, seenByKey);
+          seen.get(partition).increment(key);
         }
       }
     }
@@ -130,26 +149,11 @@ public class EndlessConsumer implements Runnable
     {
       log.info("{} - Closing the KafkaConsumer", id);
       consumer.close();
-
-      for (Integer partition : seen.keySet())
-      {
-        Map<String, Integer> byKey = seen.get(partition);
-        for (String key : byKey.keySet())
-        {
-          log.info(
-              "{} - Seen {} messages for partition={}|key={}",
-              id,
-              byKey.get(key),
-              partition,
-              key);
-        }
-      }
-
       log.info("{} - Consumer-Thread exiting", id);
     }
   }
 
-  public Map<Integer, Map<String, Integer>> getSeen()
+  public Map<TopicPartition, PartitionStatistics> getSeen()
   {
     return seen;
   }