...

/

The DSL API Stateful Operations: Join

The DSL API Stateful Operations: Join

Learn how join operations work with the Kafka Streams DSL API.

Join operations combine data from two or more input streams and/or tables based on a common key. Kafka Streams provides several types of join operations, including inner, left, and outer join, each of which has its own unique characteristics and use cases. These are used to combine related data streams and perform complex analyses and transformations on the resulting data. For example, joins can be used to combine clickstream data with customer data to create a more complete picture of customer behavior or combine data from different sensors in an IoT system to identify patterns and anomalies.

Types of join operations

Let’s explore the different types of join operations in Kafka Streams.

The KStream to KStream join

A KStream to KStream join operation is used to combine two input KStreams based on their keys. In this type of join, each record in one KStream is matched with any records in the other KStream that have the same key. The join condition is specified by a function that takes two input records and returns a boolean value indicating whether they should be joined.

Let’s say we have two KStreams: one contains information about user activity and another about user location. We want to join these two streams based on the user ID to create a stream of enriched user activity events.

Press + to interact
KStream<String, String> userActivityStream = builder.stream("activity");
KStream<String, String> userLocationStream = builder.stream("location");
KStream<String, String> enrichedStream = userActivityStream.join(
userLocationStream,
(activity, location) -> "User " + activity + " was seen at " + location
);

In the example above, we join the user activity stream with the user location stream based on the user ID key.

  • Line 4: The ValueJoiner lambda expression takes the activity and location values from the two streams and returns a string representing the enriched event. In this case, we ...

Access this course and 1400+ top-rated courses and projects.