Search in sources :

Example 1 with ConfiguredStreamBuilder

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;
}
Also used : StreamsConfig(org.apache.kafka.streams.StreamsConfig) KTable(org.apache.kafka.streams.kstream.KTable) Arrays(java.util.Arrays) Properties(java.util.Properties) Produced(org.apache.kafka.streams.kstream.Produced) Singleton(jakarta.inject.Singleton) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) KStream(org.apache.kafka.streams.kstream.KStream) Grouped(org.apache.kafka.streams.kstream.Grouped) ConfiguredStreamBuilder(io.micronaut.configuration.kafka.streams.ConfiguredStreamBuilder) Factory(io.micronaut.context.annotation.Factory) Locale(java.util.Locale) Materialized(org.apache.kafka.streams.kstream.Materialized) Serdes(org.apache.kafka.common.serialization.Serdes) Named(jakarta.inject.Named) Properties(java.util.Properties) Named(jakarta.inject.Named) Singleton(jakarta.inject.Singleton)

Example 2 with ConfiguredStreamBuilder

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;
}
Also used : StreamsConfig(org.apache.kafka.streams.StreamsConfig) KTable(org.apache.kafka.streams.kstream.KTable) Arrays(java.util.Arrays) Properties(java.util.Properties) Produced(org.apache.kafka.streams.kstream.Produced) Singleton(jakarta.inject.Singleton) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) KStream(org.apache.kafka.streams.kstream.KStream) Grouped(org.apache.kafka.streams.kstream.Grouped) ConfiguredStreamBuilder(io.micronaut.configuration.kafka.streams.ConfiguredStreamBuilder) Factory(io.micronaut.context.annotation.Factory) Locale(java.util.Locale) Materialized(org.apache.kafka.streams.kstream.Materialized) Serdes(org.apache.kafka.common.serialization.Serdes) Named(jakarta.inject.Named) Properties(java.util.Properties) Named(jakarta.inject.Named) Singleton(jakarta.inject.Singleton)

Aggregations

ConfiguredStreamBuilder (io.micronaut.configuration.kafka.streams.ConfiguredStreamBuilder)2 Factory (io.micronaut.context.annotation.Factory)2 Named (jakarta.inject.Named)2 Singleton (jakarta.inject.Singleton)2 Arrays (java.util.Arrays)2 Locale (java.util.Locale)2 Properties (java.util.Properties)2 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)2 Serdes (org.apache.kafka.common.serialization.Serdes)2 StreamsConfig (org.apache.kafka.streams.StreamsConfig)2 Grouped (org.apache.kafka.streams.kstream.Grouped)2 KStream (org.apache.kafka.streams.kstream.KStream)2 KTable (org.apache.kafka.streams.kstream.KTable)2 Materialized (org.apache.kafka.streams.kstream.Materialized)2 Produced (org.apache.kafka.streams.kstream.Produced)2