Joining streams of data
This page describes a tutorial joining Kafka topics with Lenses SQL Processors.
magine you are the next Amazon, and you want to track the orders and shipment events to work out which orders have been shipped and how long it took. In this case, there will be two data streams, one for each event type, and the resulting stream will answer the questions above.
Enriching two streams of data requires a sliding window join. The events are said to be “close” to each other, if the difference between their timestamp is up to the time window specified.
Topics involved:
orders
messages contain information about a customershipments
messages contain information about the shipment
Streaming SQL enrichment on Apache Kafka
The query combines the data from orders and shipments if the orders are processed within 24 hours. Resulting records contain the order and shipment identifier, and the time between the order was registered to the time it was shipped.
Testing data
To simplify our testing process and manage to run the above example in less than 60 seconds, we will be using SQL to create and populate the three Apache Kafka topics:
CREATE TOPIC orders
orders
POPULATE TOPIC orders
orders
CREATE TOPIC shipments
shipments
POPULATE TOPIC shipments
shipments
The output seen in the next screenshot shows two records. For the order with o2
identifier, there is no shipments
entry because it has not been processed. For the order with identifier o3
, the shipment happened after one day.
Validate results
Let’s switch to the Snapshot engine by navigating to SQL Studio
menu item. With the entries in both topics, we can write the following query to see which data is joinable without the window interval:
These are the results for the non-streaming query (i.e., Snapshot)
Running the query returned three records. But you can see the order o3
was processed two days after it was placed. Let’s apply the sliding window restriction for the Snapshot query by adding a filter to only match those records having their timestamp difference within a day.
Now the result matches the one from Streaming query.
Conclusion
In this tutorial you learned how to join to Streams together using a sliding window. You achieved all the above using Lenses SQL engine.
Good luck and happy streaming!
Last updated