Using multiple topics
This page describes a tutorial to use multiple Kafka topics in a Lenses SQL Processor.
In this tutorial, we will see how we can read data from multiple topics, process it as needed, and write the results to as many output topics we need, all by using a single SQL Processor.
Setting up our example
Let’s assume that we have a topic (game-sessions
) that contains data regarding remote gaming sessions by users.
Each gaming session will contain:
the points the user achieved throughout the session
Metadata information regarding the session:
The country where the game took place
The language the user played the game in
The above structure represents the value
of each record in our game-sessions
topic.
Additionally, each record will is keyed by user details.
A pid, or player id, representing this user uniquely
Some additional denormalised user details:
a name
a surname
an age
In light of the above, a record might look like the following (in JSON for simplicity):
Finally, let’s assume we also have another, normalised, compacted topic user-details
, keyed by an int
matching the pid
from topic game-sessions
and containing user information like address and period of membership to the platform.
In light of the above, a record might look like the following (in JSON for simplicity):
We can replicate such structures using SQL Studio and the following query:
We can then use SQL Studio again to insert the data we will use in the rest of the tutorial:
Multiple transformations all in one go
Let’s imagine that, given the above data, we are given the following requirements:
For each country in the
games-sessions
, create a record with the count of games played in from that country. Write the results to thegames-per-country
topic.For each record in the
games-sessions
, reshape the records to remove everything from the key besidepid
. Additionally, add the user’smemberYears
to the value. Write the results to thegames-sessions-normalised
topic .
We can obtain the above with the following query:
The result of this processor in the UI will be a processor graph similar to the following:
Finally, the content of the output topics games-per-country
and games-sessions-normalised
can now be inspected in the Lenses Explore screen:
Conclusion
In this tutorial, we learned how to read data from multiple topics, combine it, and process in different ways and save it in as many output topics as needed.
Good luck and happy streaming!
Last updated