Kafka Streams FK-join within the same KTable

Posted on
til kafka-streams streaming-joins dev

KTable to KTable foreign-key joins is one of the coolest features in Kafka Streams.

I was wondering whether this feature would handle FK-joins between values on the same table.

Seems that the answer is yes:

var ktable = builder.table("table", Consumed.with(Serdes.String(), Serdes.String()));
ktable
    .join(ktable, s -> s, (v1, v2) -> v1 + "-" + v2)
    .toStream()
    .to("fkjoin_v1", Produced.with(Serdes.String(), Serdes.String()));

Values will be used as keys. With the following inputs:

p1:v1
v1:v2

The following output is expected:

p1:v1-v2

For reference this is the topology graph:

Kafka Streams topology