问题
Since I am working with Json values I haven't set up default serdes.
I process a KStream, consuming it with the necessary spring and product (json) serdes, but the next step (map operation) fails:
val props = Properties()
props[StreamsConfig.APPLICATION_ID_CONFIG] = applicationName
props[StreamsConfig.BOOTSTRAP_SERVERS_CONFIG] = kafkaBootstrapServers
val productSerde: Serde<Product> = Serdes.serdeFrom(JsonPojoSerializer<Product>(), JsonPojoDeserializer(Product::class.java))
builder.stream(INVENTORY_TOPIC, Consumed.with(Serdes.String(), productSerde))
.map { key, value ->
KeyValue(key, XXX)
}
.aggregate(...)
If I remove the map operation the execution goes ok.
I haven't found a way to specify the serdes for the map(), how can it be done?
Error:
Caused by: org.apache.kafka.streams.errors.StreamsException: A serializer (key: org.apache.kafka.common.serialization.ByteArraySerializer / value: org.apache.kafka.common.serialization.ByteArraySerializer) is not compatible to the actual key or value type (key type: java.lang.String / value type: com.codependent.kafkastreams.inventory.dto.Product). Change the default Serdes in StreamConfig or provide correct Serdes via method parameters.
at org.apache.kafka.streams.processor.internals.SinkNode.process(SinkNode.java:92)
回答1:
Multiple issues:
After you call
map()
you callgroupByKey().aggregate()
. This triggers data repartition and thus aftermap()
data is written into an internal topic for data repartitioning. Therefore, you need to provide correspondingSerde
s withingroupByKey()
, too.However, because you don't modify the key, you should actually call
mapValues()
instead, to avoid the unnecessary repartitioning.Note, that you need to provide
Serde
s for each operator that should not use the defaultSerde
from the config.Serde
s are not passed along downstream, but are operator in-place overwrites. (It's work in progress for Kafka 2.1 to improve this.)
来源:https://stackoverflow.com/questions/52026148/map-operation-over-a-kstream-fails-when-not-specifying-default-serdes-and-using