X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fwordcount%2Fcounter%2FCounterStreamProcessor.java;h=64bd619d0d30d5e021e5d3be3cc59769737a3831;hb=98f4de98c6c3f527496d85b5e32fef0a3a23cbd5;hp=bccb253b0a9cb92b8863f3954b6028a6dcedb09e;hpb=cf177bd69764eb86220cdb871b37435c6c562640;p=demos%2Fkafka%2Fwordcount diff --git a/src/main/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessor.java b/src/main/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessor.java index bccb253..64bd619 100644 --- a/src/main/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessor.java +++ b/src/main/java/de/juplo/kafka/wordcount/counter/CounterStreamProcessor.java @@ -1,16 +1,12 @@ package de.juplo.kafka.wordcount.counter; -import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; -import org.apache.kafka.common.serialization.Serdes; -import org.apache.kafka.streams.KafkaStreams; -import org.apache.kafka.streams.KeyValue; -import org.apache.kafka.streams.StreamsBuilder; -import org.apache.kafka.streams.Topology; +import org.apache.kafka.streams.*; import org.apache.kafka.streams.kstream.KStream; import org.apache.kafka.streams.kstream.Materialized; import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier; -import org.apache.kafka.streams.kstream.*; +import org.apache.kafka.streams.state.QueryableStoreTypes; +import org.apache.kafka.streams.state.ReadOnlyKeyValueStore; import org.springframework.kafka.support.serializer.JsonSerde; import java.util.Properties; @@ -19,6 +15,9 @@ import java.util.Properties; @Slf4j public class CounterStreamProcessor { + public static final String STORE_NAME = "counter"; + + public final KafkaStreams streams; @@ -26,14 +25,12 @@ public class CounterStreamProcessor String inputTopic, String outputTopic, Properties properties, - KeyValueBytesStoreSupplier storeSupplier, - ObjectMapper mapper) + KeyValueBytesStoreSupplier storeSupplier) { Topology topology = CounterStreamProcessor.buildTopology( inputTopic, outputTopic, - storeSupplier, - mapper); + storeSupplier); streams = new KafkaStreams(topology, properties); } @@ -41,37 +38,22 @@ public class CounterStreamProcessor static Topology buildTopology( String inputTopic, String outputTopic, - KeyValueBytesStoreSupplier storeSupplier, - ObjectMapper mapper) + KeyValueBytesStoreSupplier storeSupplier) { StreamsBuilder builder = new StreamsBuilder(); - KStream source = builder.stream( - inputTopic, - Consumed.with( - Serdes.String(), - new JsonSerde<>(Word.class) - .ignoreTypeHeaders())); + KStream source = builder.stream(inputTopic); source .map((key, word) -> new KeyValue<>(word, word)) - .groupByKey(Grouped.with( - new JsonSerde<>(Word.class) - .forKeys() - .noTypeInfo(), - new JsonSerde<>(Word.class) - .noTypeInfo())) - .count(Materialized.as(storeSupplier)) + .groupByKey() + .count( + Materialized + .as(storeSupplier) + .withKeySerde(new JsonSerde<>().copyWithType(Word.class).forKeys())) .toStream() - .map((word, count) -> new KeyValue<>(word, WordCount.of(word.getUser(), word.getWord(), count))) - .to( - outputTopic, - Produced.with( - new JsonSerde<>(Word.class) - .forKeys() - .noTypeInfo(), - new JsonSerde<>(WordCount.class) - .noTypeInfo())); + .map((word, counter) -> new KeyValue<>(word, WordCounter.of(word, counter))) + .to(outputTopic); Topology topology = builder.build(); log.info("\n\n{}", topology.describe()); @@ -79,6 +61,11 @@ public class CounterStreamProcessor return topology; } + ReadOnlyKeyValueStore getStore() + { + return streams.store(StoreQueryParameters.fromNameAndType(STORE_NAME, QueryableStoreTypes.keyValueStore())); + } + public void start() { log.info("Starting Stream-Processor");