Die Key-Statistiken werden in einer MongoDB gespeichert
authorKai Moritz <kai@juplo.de>
Wed, 6 Apr 2022 18:54:37 +0000 (20:54 +0200)
committerKai Moritz <kai@juplo.de>
Wed, 6 Apr 2022 21:09:56 +0000 (23:09 +0200)
docker-compose.yml
pom.xml
src/main/java/de/juplo/kafka/Application.java
src/main/java/de/juplo/kafka/EndlessConsumer.java
src/main/java/de/juplo/kafka/KeyCounter.java
src/main/java/de/juplo/kafka/PartitionStatistics.java
src/main/java/de/juplo/kafka/PartitionStatisticsRepository.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/StatisticsDocument.java [new file with mode: 0644]
src/main/resources/application.yml

index 0b1f0ae..3428d16 100644 (file)
@@ -24,6 +24,14 @@ services:
     depends_on:
       - zookeeper
 
+  mongo:
+    image: mongo:4.4
+    ports:
+      - 27017:27017
+    environment:
+      MONGO_INITDB_ROOT_USERNAME: juplo
+      MONGO_INITDB_ROOT_PASSWORD: training
+
   kafka-ui:
     image: provectuslabs/kafka-ui:0.3.3
     ports:
@@ -53,6 +61,7 @@ services:
       - 8081:8081
     environment:
       consumer.bootstrap-server: kafka:9092
-      consumer.client-id: my-group
       consumer.client-id: consumer
       consumer.topic: test
+      spring.data.mongodb.uri: mongodb://juplo:training@mongo:27017
+      spring.data.mongodb.database: juplo
diff --git a/pom.xml b/pom.xml
index b7b0b8d..78b2fde 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-data-mongodb</artifactId>
+    </dependency>
     <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
index 7cfe268..23c845a 100644 (file)
@@ -20,7 +20,7 @@ public class Application
 
 
   @Bean
-  public EndlessConsumer consumer()
+  public EndlessConsumer consumer(PartitionStatisticsRepository repository)
   {
     Assert.hasText(properties.getBootstrapServer(), "consumer.bootstrap-server must be set");
     Assert.hasText(properties.getGroupId(), "consumer.group-id must be set");
@@ -30,6 +30,7 @@ public class Application
     EndlessConsumer consumer =
         new EndlessConsumer(
             Executors.newFixedThreadPool(1),
+            repository,
             properties.getBootstrapServer(),
             properties.getGroupId(),
             properties.getClientId(),
index c7bc852..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;
@@ -38,6 +39,7 @@ public class EndlessConsumer implements Runnable
 
   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;
@@ -87,6 +90,7 @@ public class EndlessConsumer implements Runnable
                   removed.getPartition(),
                   counter.getKey());
             }
+            repository.save(new StatisticsDocument(removed));
           });
         }
 
@@ -96,7 +100,12 @@ public class EndlessConsumer implements Runnable
           partitions.forEach(tp ->
           {
             log.info("{} - adding partition: {}", id, tp);
-            seen.put(tp, new PartitionStatistics(tp));
+            seen.put(
+                tp,
+                repository
+                    .findById(tp.toString())
+                    .map(PartitionStatistics::new)
+                    .orElse(new PartitionStatistics(tp)));
           });
         }
       });
index b2cde47..1e3cbd2 100644 (file)
@@ -17,6 +17,13 @@ public class KeyCounter
   private long result = 0;
 
 
+  public KeyCounter(String key, long initialValue)
+  {
+    this.key = key;
+    this.result = initialValue;
+  }
+
+
   public long increment()
   {
     return ++result;
index e47a9f9..0e31945 100644 (file)
@@ -2,7 +2,6 @@ package de.juplo.kafka;
 
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
-import lombok.RequiredArgsConstructor;
 import org.apache.kafka.common.TopicPartition;
 
 import java.util.Collection;
@@ -10,15 +9,35 @@ import java.util.HashMap;
 import java.util.Map;
 
 
-@RequiredArgsConstructor
 @Getter
 @EqualsAndHashCode(of = { "partition" })
 public class PartitionStatistics
 {
+  private String id;
   private final TopicPartition partition;
   private final Map<String, KeyCounter> statistics = new HashMap<>();
 
 
+  public PartitionStatistics(TopicPartition partition)
+  {
+    this.partition = partition;
+  }
+
+  public PartitionStatistics(StatisticsDocument document)
+  {
+    this.partition = new TopicPartition(document.topic, document.partition);
+    document
+        .statistics
+        .entrySet()
+        .forEach(entry ->
+        {
+          this.statistics.put(
+              entry.getKey(),
+              new KeyCounter(entry.getKey(), entry.getValue()));
+        });
+  }
+
+
   public KeyCounter addKey(String key)
   {
     KeyCounter counter = new KeyCounter(key);
diff --git a/src/main/java/de/juplo/kafka/PartitionStatisticsRepository.java b/src/main/java/de/juplo/kafka/PartitionStatisticsRepository.java
new file mode 100644 (file)
index 0000000..0ccf3cd
--- /dev/null
@@ -0,0 +1,11 @@
+package de.juplo.kafka;
+
+import org.springframework.data.mongodb.repository.MongoRepository;
+
+import java.util.Optional;
+
+
+public interface PartitionStatisticsRepository extends MongoRepository<StatisticsDocument, String>
+{
+  public Optional<StatisticsDocument> findById(String partition);
+}
diff --git a/src/main/java/de/juplo/kafka/StatisticsDocument.java b/src/main/java/de/juplo/kafka/StatisticsDocument.java
new file mode 100644 (file)
index 0000000..9318c4c
--- /dev/null
@@ -0,0 +1,39 @@
+package de.juplo.kafka;
+
+import lombok.ToString;
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+import java.util.HashMap;
+import java.util.Map;
+
+
+@Document(collection = "statistics")
+@ToString
+public class StatisticsDocument
+{
+  @Id
+  public String id;
+  public String topic;
+  public Integer partition;
+  public Map<String, Long> statistics;
+
+  public StatisticsDocument()
+  {
+  }
+
+  public StatisticsDocument(String topic, Integer partition, Map<String, Long> statistics)
+  {
+    this.partition = partition;
+    this.statistics = statistics;
+  }
+
+  public StatisticsDocument(PartitionStatistics statistics)
+  {
+    this.topic = statistics.getPartition().topic();
+    this.id = statistics.toString();
+    this.partition = statistics.getPartition().partition();
+    this.statistics = new HashMap<>();
+    statistics.getStatistics().forEach(counter -> this.statistics.put(counter.getKey(), counter.getResult()));
+  }
+}
index db37822..0e7c53c 100644 (file)
@@ -9,6 +9,11 @@ management:
     web:
       exposure:
         include: "*"
+spring:
+  data:
+    mongodb:
+      uri: mongodb://juplo:training@localhost:27017
+      database: juplo
 logging:
   level:
     root: INFO