Handling bad messages using Kafka's Streams API

前端 未结 4 512
故里飘歌
故里飘歌 2020-11-30 19:21

I have a basic stream processing flow which looks like

master topic -> my processing in a mapper/filter -> output topics

and I am won

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 19:44

    For the processing logic you could take this approach:

    someKStream 
    
        .mapValues(inputValue -> {
            // for each execution the below "return" could provide a different class than the previous run!
            // e.g. "return isFailedProcessing ? failValue : successValue;" 
            // where failValue and successValue have no related classes
            return someObject; // someObject class vary at runtime depending on your business
        }) // here you'll have KStream -> yes, Object for the value!
    
        // you could have a different logic for choosing  
        // the target topic, below is just an example
        .to((k, v, recordContext) -> v instanceof failValueClass ?
                "dead-letter-topic" : "success-topic",
                // you could completelly ignore the "Produced" part 
                // and rely on spring-boot properties only, e.g. 
                // spring.kafka.streams.properties.default.key.serde=yourKeySerde
                // spring.kafka.streams.properties.default.value.serde=org.springframework.kafka.support.serializer.JsonSerde
                Produced.with(yourKeySerde, 
                                // JsonSerde could be an instance configured as you need 
                                // (with type mappings or headers setting disabled, etc)
                                new JsonSerde<>())); 
    

    Your classes, though different and landing into different topics, will serialize as expected.

    When not using to(), but instead one wants to continue with other processing, he could use branch() with splitting the logic based on the kafka-value class; the trick for branch() is to return KStream[] in order to further allow one to cast to the appropriate class the individual array items.

提交回复
热议问题