1 package de.juplo.kafka.wordcount.counter;
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import lombok.extern.slf4j.Slf4j;
6 import org.apache.kafka.streams.KafkaStreams;
7 import org.apache.kafka.streams.KeyValue;
8 import org.apache.kafka.streams.StreamsBuilder;
9 import org.apache.kafka.streams.kstream.KStream;
11 import java.util.Properties;
15 public class CounterStreamProcessor
17 public final KafkaStreams streams;
20 public CounterStreamProcessor(
23 Properties properties,
26 StreamsBuilder builder = new StreamsBuilder();
28 KStream<String, String> source = builder.stream(inputTopic);
30 .map((username, word) ->
34 String key = mapper.writeValueAsString(Key.of(username, word));
35 return new KeyValue<>(key, word);
37 catch (JsonProcessingException e)
39 throw new RuntimeException(e);
44 .mapValues(value->Long.toString(value))
48 streams = new KafkaStreams(builder.build(), properties);
53 log.info("Starting Stream-Processor");
59 log.info("Stopping Stream-Processor");