I am unable to change the Serde of a channel (or a binding) using the syntax specified in the documentation (https://docs.spring.io/spring-cloud-stream/docs/Elmhurst.RELEASE/reference/htmlsingle/#_configuration_options_3).
Assuming my channel is pcin, I understand that I should indicate valueSerde and keySerde using the follwing properties spring.cloud.stream.kafka.streams.bindings.pcin.producer.valueSerde and spring.cloud.stream.kafka.streams.bindings.pcin.producer.keySerde.
However, I am receiving an exception :
Caused by: org.apache.kafka.streams.errors.StreamsException: A serializer (key: org.apache.kafka.common.serialization.StringSerializer / value: org.apache.kafka.common.serialization.StringSerializer) is not compatible to the actual key or value type (key type: java.lang.String / value type: java.lang.Long). Change the default Serdes in StreamConfig or provide correct Serdes via method parameters.
I am trying to adapt the example from Spring Tips, by Josh Long : https://github.com/spring-tips/spring-cloud-stream-kafka-streams
I just changed the class PageViewEventProcessor as follows:
@Component
public static class PageViewEventProcessor {
@StreamListener
@SendTo(AnalyticsBinding.PAGE_COUNT_OUT)
public KStream<String, Long> process(@Input(AnalyticsBinding.PAGE_VIEWS_IN) KStream<String, PageViewEvent> events) {
return events
.filter((key, value) -> value.getDuration() > 10)
.map((key, value) -> new KeyValue<>(value.getPage(), value.getDuration()))
.groupByKey()
.aggregate(()-> 0L,
(cle, val, valAgregee) -> valAgregee + val,
Materialized.as(AnalyticsBinding.PAGE_COUNT_MV))
.toStream();
}
}
Instead of counting the number of events (page visits), I calculate the sum of durations of each visit.
Here is an extract of application.properties (from Spring tips sample):
# page counts out
spring.cloud.stream.bindings.pcout.destination=pcs
spring.cloud.stream.bindings.pcout.producer.use-native-encoding=true
spring.cloud.stream.kafka.streams.bindings.pcout.producer.key-serde=org.apache.kafka.common.serialization.Serdes$StringSerde
spring.cloud.stream.kafka.streams.bindings.pcout.producer.value-serde=org.apache.kafka.common.serialization.Serdes$LongSerde
#
# page counts in
spring.cloud.stream.bindings.pcin.destination=pcs
spring.cloud.stream.bindings.pcin.consumer.use-native-decoding=true
spring.cloud.stream.bindings.pcin.group=pcs
spring.cloud.stream.bindings.pcin.content-type=application/json
spring.cloud.stream.bindings.pcin.consumer.header-mode=raw
spring.cloud.stream.kafka.streams.bindings.pcin.consumer.key-serde=org.apache.kafka.common.serialization.Serdes$StringSerde
spring.cloud.stream.kafka.streams.bindings.pcin.consumer.value-serde=org.apache.kafka.common.serialization.Serdes$LongSerde
Are there any other required changes?