I'm trying to implement a use case in Kafka Streams where I populate a Ktable based on applying some filters on this stream, let's call this table a tracking table where the key is derived from the event and the value is the event. Now for subsequent events I check this table to verify if they're tracked and update the event if it's tracked or publish it to a different topic. I'm not sure how to do this simultaneously. Here's what I have so far.
// Branch based on conditions
KStream<String, Event>[] segregatedRecords = branches[0]
.branch((key, event) -> event.getStatus().getStatus().equals("A"),
(key, event) -> event.getStatus().getStatus().equals("B"),
(key, event) -> event.getStatus().getStatus().equals("C"),
// Store events with status A to a topic
segregatedRecords[0]
.selectKey((key, event) -> createKey(event))
.mapValues(transform)
.to(intermediateTopic);
// Load topic from previous step as GlobalKtable
GlobalKTable<String, Event> trackedEvents = streamsBuilder.globalTable(intermediateTopic);
// The following step is where I'm stuck, because I can not perform conditional actions
// If the event exists in the tracking table (update) but if not then how to publish it to a different topic?
segregatedRecords[1]
// derive key for lookup
.selectKey((key, event) -> createKey(event))
// update the event status in the table
.join(trackedEvents, (key, value) -> key,(event, tracked) -> modifiedEvent
).to(intermediateTopic);
// Other events will need to refer the latest information in the tracked table for further processing