title: Kafka Streams: How to transform message before publishing to different topics
body: <p>Messages on Topic A are read using Kafka Streams. Based on some validation, the message is published to either Topic B or Topic C. This was easily achieved using Kafka Streams' <a href="https://kafka.apache.org/11/javadoc/org/apache/kafka/streams/kstream/KStream.html#branch-org.apache.kafka.streams.kstream.Predicate...-" rel="nofollow noreferrer">branch</a> method</p>
  
  <pre><code>KStreamBuilder kStreamBuilder = new KStreamBuilder();
  KStream&lt;String, String&gt; kStream = kStreamBuilder.stream(Serdes.String(), Serdes.String(), topicA.split(","));
  
  KStream&lt;String, String&gt;[] splitStreams = kStream.branch(
      (key, value) -&gt; process(value), //Process method is expensive
      (key, value) -&gt; true
  );
  
  splitStreams[0].to(Serdes.String(), Serdes.String(), topicB);
  splitStreams[1].to(Serdes.String(), Serdes.String(), topicC);
  </code></pre>
  
  <p>Problem: Based on whether the message passes or fails validation, it is to be edited/appended before publishing it to either Topic B or Topic C. How do
  I achieve that? </p>