counter: 1.3.1 - Refined/Simplified the type-mapping
authorKai Moritz <kai@juplo.de>
Sun, 16 Jun 2024 17:57:23 +0000 (19:57 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 16 Jun 2024 19:06:59 +0000 (21:06 +0200)
* Removed all explicit type-mappings for the internally used types.
* This greatly simplifies the configuration, because it is sufficient to
  configure the trusted package to serialize/deserialize all internally
  used types.
* To make this possible, the type-mappings for the outgoing messages are
  specified with `Produced.with()` in the ``to()``-operation.

src/main/java/de/juplo/kafka/wordcount/counter/CounterApplicationConfiguriation.java
src/main/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessor.java

index d9579a5..174521f 100644 (file)
@@ -50,11 +50,7 @@ public class CounterApplicationConfiguriation
 
                propertyMap.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
                propertyMap.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, JsonSerde.class.getName());
-               propertyMap.put(
-                               JsonDeserializer.TYPE_MAPPINGS,
-                               "user:" + User.class.getName() + "," +
-                               "word:" + Word.class.getName() + "," +
-                               "counter:" + WordCounter.class.getName());
+               propertyMap.put(JsonDeserializer.TRUSTED_PACKAGES, CounterApplication.class.getPackageName());
 
                return propertyMap;
        }
index 8b9c12b..97d460f 100644 (file)
@@ -4,12 +4,16 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.streams.*;
 import org.apache.kafka.streams.kstream.Consumed;
 import org.apache.kafka.streams.kstream.Materialized;
+import org.apache.kafka.streams.kstream.Produced;
 import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
 import org.apache.kafka.streams.state.QueryableStoreTypes;
 import org.apache.kafka.streams.state.ReadOnlyKeyValueStore;
 import org.springframework.kafka.support.serializer.JsonSerde;
+import org.springframework.kafka.support.serializer.JsonSerializer;
 
+import java.util.Map;
 import java.util.Properties;
+import java.util.stream.Collectors;
 
 
 @Slf4j
@@ -56,7 +60,7 @@ public class CounterStreamProcessor
                                                                .withKeySerde(new JsonSerde<>().copyWithType(Word.class).forKeys()))
                                .toStream()
                                .map((word, counter) -> new KeyValue<>(word, WordCounter.of(word, counter)))
-                               .to(outputTopic);
+                               .to(outputTopic, Produced.with(outKeySerde(), outValueSerde()));
 
                Topology topology = builder.build();
                log.info("\n\n{}", topology.describe());
@@ -80,4 +84,41 @@ public class CounterStreamProcessor
                log.info("Stopping Stream-Processor");
                streams.close();
        }
+
+
+
+       public static JsonSerde<Word> outKeySerde()
+       {
+               return serde(true);
+       }
+
+       public static JsonSerde<WordCounter> outValueSerde()
+       {
+               return serde(false);
+       }
+
+       public static <T> JsonSerde<T> serde(boolean isKey)
+       {
+               JsonSerde<T> serde = new JsonSerde<>();
+               serde.configure(
+                               Map.of(JsonSerializer.TYPE_MAPPINGS, typeMappingsConfig()),
+                               isKey);
+               return serde;
+       }
+
+       private static String typeMappingsConfig()
+       {
+               return typeMappings()
+                               .entrySet()
+                               .stream()
+                               .map(entry -> entry.getKey() + ":" + entry.getValue().getName())
+                               .collect(Collectors.joining(","));
+       }
+
+       private static Map<String, Class> typeMappings()
+       {
+               return Map.of(
+                               "word", Word.class,
+                               "counter", WordCounter.class);
+       }
 }