- The default store-type creates state, that is stored on disk.
- Hence, only the first run of the test succseeded.
- The bug was fixed by providing an in-memory store-type.
</parent>
<groupId>de.juplo.kafka.wordcount</groupId>
<artifactId>counter</artifactId>
- <version>1.1.4</version>
+ <version>1.1.5</version>
<name>Wordcount-Counter</name>
<description>Word-counting stream-processor of the multi-user wordcount-example</description>
<properties>
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.StreamsConfig;
+import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
+import org.apache.kafka.streams.state.Stores;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@Bean(initMethod = "start", destroyMethod = "stop")
public CounterStreamProcessor streamProcessor(
CounterApplicationProperties properties,
+ KeyValueBytesStoreSupplier storeSupplier,
ObjectMapper objectMapper,
ConfigurableApplicationContext context)
{
properties.getInputTopic(),
properties.getOutputTopic(),
propertyMap,
+ storeSupplier,
objectMapper);
streamProcessor.streams.setUncaughtExceptionHandler((Throwable e) ->
return streamProcessor;
}
+ @Bean
+ public KeyValueBytesStoreSupplier storeSupplier()
+ {
+ return Stores.persistentKeyValueStore("counter");
+ }
+
public static void main(String[] args)
{
SpringApplication.run(CounterApplication.class, args);
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.kstream.KStream;
+import org.apache.kafka.streams.kstream.Materialized;
+import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
import java.util.Properties;
String inputTopic,
String outputTopic,
Properties properties,
+ KeyValueBytesStoreSupplier storeSupplier,
ObjectMapper mapper)
{
StreamsBuilder builder = new StreamsBuilder();
}
})
.groupByKey()
- .count()
+ .count(Materialized.as(storeSupplier))
.mapValues(value->Long.toString(value))
.toStream()
.to(outputTopic);
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier;
+import org.apache.kafka.streams.state.Stores;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Primary;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.test.context.EmbeddedKafka;
{
return new Consumer(mapper);
}
+
+ @Primary
+ @Bean
+ KeyValueBytesStoreSupplier inMemoryStoreSupplier()
+ {
+ return Stores.inMemoryKeyValueStore("TEST-STORE");
+ }
}
}