This page describe a tutorial to change the shape (fields) of data in a Kafka topic using Lenses SQL Processors.
In this tutorial, we will see how to use Lenses SQL to alter the shape of your records.
In Lenses SQL you can quickly reshape your data with a simple SELECT
and some built-in functions.
We will learn how to
put value fields into the key
lift key fields into the value
call functions to transform your data
build nested structures for your keys/values
unwrap singleton objects into primitive types
In our example, we are getting data from speed sensors from a speed car circuit.
The upstream system registers speed measurement events as records in a Kafka topic.
An example of such an event is the following:
We can replicate such a structure running the following query in SQL Studio:
Each event is keyed by a unique string generated by the upstream system.
We can again use SQL Studio to insert some data to play with:
In this section, we are only interested in the speed of single cars, and we do not care about all the other fields.
We want to use the car id, which now is part of the record Value, to become the new key (using the special as _key
syntax). We also want the car speed as the new record Value.
To achieve that we can create a new SQL Processor using some simple projections.
Checking the records emitted by the processor we see that the shape of the records is
We want to avoid that intermediate wrapping of speedMph
inside an object. To do that we can tell Lenses to unwrap the value with the special as _value
syntax, saving some bytes and CPU cycles:
Now the shape of the records is what we had in mind:
This time we want to do some more complex manipulation. We want to convert the speed from Mph to Kmph, and we would also want to build a nice string describing the event.
An example of an output record would be:
In this case, we are using CONCATENATE
to concatenate multiple strings, CAST
to convert an expression to another type, and *
, the usual multiplication operator.
If we check the resulting records, we can see that we obtained the shape we were looking for. Please note that the keys have been left untouched:
In this last example, we will show how to create composite keys and values in our projections.
We want both the sensor id
and the event_time
as the record Key. For the record Value, we want the car_id
and the speed, expressed both as Mph and Kmph.
Lenses SQL allows as to use nested aliases to build nested structures. You have to put some dots in your aliases.
The resulting shape of the record is what we were aiming for:
Happy re-shaping!