How to split records into different streams, from one topic to different streams?

前端 未结 2 432
梦毁少年i
梦毁少年i 2021-01-27 09:22

I have a single source CSV file containing records of different sizes that pushes every record into one source topic. I want to split the records into different KStreams/KTables

2条回答
  •  独厮守ぢ
    2021-01-27 09:33

    You can write a separated Kafka Streams application to split records from the input topic to different KStream or output topics using KStream#branch() operator:

    KStream[] branches = streamsBuilder.branch(
            (key, value) -> {filter logic for topic 1 here},
            (key, value) -> {filter logic for topic 2 here},
            (key, value) -> true//get all messages for this branch
    );
    
    // KStream branches[0] records for logic 1
    // KStream branches[1] records for logic 2
    // KStream branches[2] records for logic 3
    

    Or you could manually branch your KStream like this:

    KStream inputKStream = streamsBuilder.stream("your_input_topic", Consumed.with(keySerde, valueSerdes));
    
    inputKStream
            .filter((key, value) -> {filter logic for topic 1 here})
            .to("your_1st_output_topic");
    
    inputKStream
            .filter((key, value) -> {filter logic for topic 2 here})
            .to("your_2nd_output_topic");
    ...
    

提交回复
热议问题