X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2FEndlessConsumer.java;h=25632048aaa0bf637bfacb9acdeb4ea11aab30b6;hb=620191782035383e0083dc348e4941c9cec0d994;hp=b152310b3441316032b59ba8ff7948d3291c8c84;hpb=3c1d3fa68df685146bdef7cc2e396e55fa0933dc;p=demos%2Fkafka%2Ftraining diff --git a/src/main/java/de/juplo/kafka/EndlessConsumer.java b/src/main/java/de/juplo/kafka/EndlessConsumer.java index b152310..2563204 100644 --- a/src/main/java/de/juplo/kafka/EndlessConsumer.java +++ b/src/main/java/de/juplo/kafka/EndlessConsumer.java @@ -34,7 +34,7 @@ public class EndlessConsumer implements Runnable private KafkaConsumer consumer = null; private Future future = null; - private final Map seen = new HashMap<>(); + private final Map> 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 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 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 getSeen() + public Map> getSeen() { return seen; }