Use lambda functions¶
Context¶
You want to transform a column with structured data in a particular way, but there doesn't exist a built-in function that suits your needs and you're unable to implement and deploy a user-defined function. ksqlDB is capable of composing existing functions to create new expressions over structured data. These are called lambda functions.
In action¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Syntax¶
The arguments for the lambda function are separated from the body of the lambda with the lambda operator, =>
.
When there are two or more arguments, you must enclose the arguments with parentheses. Parentheses are optional for lambda functions with one argument.
Currently, ksqlDB supports up to three arguments in a single lambda function.
1 2 3 4 5 |
|
Invocation UDFs¶
Lambda functions must be used inside designated invocation functions. These are the available Invocations:
Create a lambda-compatible stream¶
Invocation functions require either a map or array input. The following example creates a stream
with a column type of MAP<STRING, INTEGER>
.
1 2 3 4 5 6 7 8 |
|
Apply a lambda invocation function¶
A lambda invocation function is a scalar UDF, and you use it like other scalar functions.
The following example lambda function transforms both the key and value of a map and produces a new map. A built-in UDF transforms the key
into an uppercase string using a built-in UDF, and the value is transformed through addition. The order of the variables
is important: the first item in the arguments list, named k
in this example, is treated as the key, and the second,
named v
in this example, is treated as the value. Pay attention to this if your map has different types.
Note that transform
on a map requires two lambda functions, while transform
on an array requires one.
1 2 3 4 |
|
Insert some values into stream1
.
1 2 3 4 5 |
|
Query the output. Make sure to set auto.offset.reset = earliest
.
1 |
|
Your output should resemble:
1 2 3 4 5 |
|
Use a reduce lambda invocation function¶
The following example creates a stream with a column type ARRAY<INTEGER>
and applies the reduce
lambda
invocation function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
stream2
.
1 2 3 4 5 |
|
Query the output. Make sure to set auto.offset.reset = earliest
.
1 |
|
You should see something similar to:
1 2 3 4 |
|
Use a filter lambda invocation function¶
Create a stream with a column type MAP<STRING, INTEGER>
and apply the filter
lambda
invocation function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
stream3
.
1 2 3 4 5 |
|
Query the output. Make sure to set auto.offset.reset = earliest
.
1 |
|
Your output should resemble:
1 2 3 4 |
|
Advanced lambda use cases¶
The following example creates a stream with a column type MAP<STRING, ARRAY<DECIMAL(2,3)>
and applies the transform
lambda invocation function with a nested transform
lambda invocation function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
stream4
.
1 2 3 4 5 |
|
Query the output. Make sure to set auto.offset.reset = earliest
.
1 |
|
Your output should resemble:
1 2 3 4 |
|