query: 2.0.0 - Values are serialized as JSON
authorKai Moritz <kai@juplo.de>
Tue, 11 Jun 2024 18:41:30 +0000 (20:41 +0200)
committerKai Moritz <kai@juplo.de>
Thu, 13 Jun 2024 15:24:00 +0000 (17:24 +0200)
--
works, still startling, but explainable

* Splitting the command ``table()``, that reads the input-topic and
  materializes it as ``KTable()``, into the two statements ``stream()``,
  that reads the input-topic into a ``KStream``, and ``toTable()``, that
  turns the `KStream` into a ``KTable``, the ``JsonSerde``, that is
  specified via ``Consumed.with(..)``, is only used for the serialization
  and deserialization concerning the ``KTable`` -- not the deserialization
  of the values, that are read from the input-topic.
* Hence, the type-mappings does not have to be specified for the
  ``JsonSerde``, resulting in better understandable code.
* __Note__, that the resulting topology does not differe, because the
  DSL is able to combine the effects of the two statements.

src/main/java/de/juplo/kafka/wordcount/query/QueryStreamProcessor.java

index 0692652..7dacd4b 100644 (file)
@@ -5,7 +5,6 @@ import jakarta.annotation.PreDestroy;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.common.serialization.Serdes;
 import org.apache.kafka.streams.*;
-import org.apache.kafka.streams.kstream.Consumed;
 import org.apache.kafka.streams.kstream.KStream;
 import org.apache.kafka.streams.kstream.KTable;
 import org.apache.kafka.streams.kstream.Materialized;
@@ -13,11 +12,9 @@ import org.apache.kafka.streams.state.HostInfo;
 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.JsonDeserializer;
 import org.springframework.kafka.support.serializer.JsonSerde;
 
 import java.net.URI;
-import java.util.Map;
 import java.util.Optional;
 import java.util.Properties;
 
@@ -55,18 +52,9 @@ public class QueryStreamProcessor
        {
                StreamsBuilder builder = new StreamsBuilder();
 
-               JsonSerde valueSerde = new JsonSerde();
-               valueSerde.configure(Map.of(
-                               JsonDeserializer.TYPE_MAPPINGS,
-                               "user:" + Key.class.getName() + "," +
-                                               "ranking:" + Ranking.class.getName() + "," +
-                                               "userdata:" + User.class.getName() + "," +
-                                               "userranking:" + UserRanking.class.getName()
-               ), false);
-               KTable<String, User> users = builder.table(
-                               usersInputTopic,
-                               Consumed.with(null, valueSerde.copyWithType(User.class))
-                               );
+               KTable<String, User> users = builder
+                               .stream(usersInputTopic)
+                               .toTable(Materialized.with(null, new JsonSerde().copyWithType(User.class)));
                KStream<String, Ranking> rankings = builder.stream(rankingInputTopic);
 
                rankings