Merge branch 'stored-state' into stored-offsets
[demos/kafka/training] / src / main / java / de / juplo / kafka / EndlessConsumer.java
index b152310..2563204 100644 (file)
@@ -34,7 +34,7 @@ public class EndlessConsumer implements Runnable
   private KafkaConsumer<String, String> consumer = null;
   private Future<?> future = null;
 
-  private final Map<TopicPartition, PartitionStatistics> seen = new HashMap<>();
+  private final Map<Integer, Map<String, Integer>> seen = new HashMap<>();
 
 
   public EndlessConsumer(
@@ -81,17 +81,17 @@ public class EndlessConsumer implements Runnable
           partitions.forEach(tp ->
           {
             log.info("{} - removing partition: {}", id, tp);
-            PartitionStatistics removed = seen.remove(tp);
-            for (KeyCounter counter : removed.getStatistics())
+            Map<String, Integer> removed = seen.remove(tp.partition());
+            for (String key : removed.keySet())
             {
               log.info(
                   "{} - Seen {} messages for partition={}|key={}",
                   id,
-                  counter.getResult(),
-                  removed.getPartition(),
-                  counter.getKey());
+                  removed.get(key),
+                  tp.partition(),
+                  key);
             }
-            repository.save(new StatisticsDocument(removed, consumer.position(tp)));
+            repository.save(new StatisticsDocument(tp.partition(), removed, consumer.position(tp)));
           });
         }
 
@@ -103,10 +103,10 @@ public class EndlessConsumer implements Runnable
             log.info("{} - adding partition: {}", id, tp);
             StatisticsDocument document =
                 repository
-                    .findById(tp.toString())
-                    .orElse(new StatisticsDocument(tp));
+                    .findById(Integer.toString(tp.partition()))
+                    .orElse(new StatisticsDocument(tp.partition()));
             consumer.seek(tp, document.offset);
-            seen.put(tp, new PartitionStatistics(document));
+            seen.put(tp.partition(), document.statistics);
           });
         }
       });
@@ -131,12 +131,23 @@ public class EndlessConsumer implements Runnable
               record.value()
           );
 
-          TopicPartition partition = new TopicPartition(record.topic(), record.partition());
+          Integer partition = record.partition();
           String key = record.key() == null ? "NULL" : record.key();
-          seen.get(partition).increment(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.forEach((tp, statistics) -> repository.save(new StatisticsDocument(statistics, consumer.position(tp))));
+        seen.forEach((partiton, statistics) -> repository.save(
+            new StatisticsDocument(
+                partiton,
+                statistics,
+                consumer.position(new TopicPartition(topic, partiton)))));
       }
     }
     catch(WakeupException e)
@@ -156,7 +167,7 @@ public class EndlessConsumer implements Runnable
     }
   }
 
-  public Map<TopicPartition, PartitionStatistics> getSeen()
+  public Map<Integer, Map<String, Integer>> getSeen()
   {
     return seen;
   }