use of io.micronaut.configuration.kafka.streams.ConfiguredStreamBuilder in project micronaut-kafka by micronaut-projects.
the class WordCountStream method myStream.
@Singleton
@Named(MY_STREAM)
KStream<String, String> myStream(@Named(MY_STREAM) ConfiguredStreamBuilder builder) {
// end::namedStream[]
// set default serdes
Properties props = builder.getConfiguration();
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
KStream<String, String> source = builder.stream(NAMED_WORD_COUNT_INPUT);
KTable<String, Long> counts = source.flatMapValues(value -> Arrays.asList(value.toLowerCase(Locale.getDefault()).split(" "))).groupBy((key, value) -> value).count();
// need to override value serde to Long type
counts.toStream().to(NAMED_WORD_COUNT_OUTPUT, Produced.with(Serdes.String(), Serdes.Long()));
return source;
}
use of io.micronaut.configuration.kafka.streams.ConfiguredStreamBuilder in project micronaut-kafka by micronaut-projects.
the class WordCountStream method wordCountStream.
// end::clazz[]
// tag::wordCountStream[]
@Singleton
@Named(STREAM_WORD_COUNT)
KStream<String, String> wordCountStream(ConfiguredStreamBuilder builder) {
// <3>
// set default serdes
Properties props = builder.getConfiguration();
props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
KStream<String, String> source = builder.stream(INPUT);
KTable<String, Long> groupedByWord = source.flatMapValues(value -> Arrays.asList(value.toLowerCase().split("\\W+"))).groupBy((key, word) -> word, Grouped.with(Serdes.String(), Serdes.String())).count(// <4>
Materialized.as(WORD_COUNT_STORE));
groupedByWord.toStream().to(OUTPUT, Produced.with(Serdes.String(), Serdes.Long()));
return source;
}
Aggregations