Click to copy
How to Split Strings into Parts in ksqlDB
ksqlDB provides powerful string manipulation capabilities to break strings into smaller parts. This allows you to extract specific portions of string data for analysis or filtering.
Here are two easy ways to split strings in ksqlDB:
The SPLIT
function lets you split a string into an array of substrings based on a delimiter. For example:
SPLIT(column, ',')
This would split the values in column
on commas into an array.
You can then access the array elements to extract the split values:
SPLIT(column, ',')[0] -- Get first split value
SPLIT(column, ',')[1] -- Get second split value
You can also use regular expressions with the REGEXP_SPLIT
function.
For example:
REGEXP_SPLIT(column, '\d{3}')
This splits the string on any 3-digit number.
The regular expression provides maximum flexibility to match custom delimiters.
Here's a full example that splits a string into first and last name:
SELECT
REGEXP_SPLIT(name, '\s+')[0] AS first_name,
REGEXP_SPLIT(name, '\s+')[1] AS last_name
FROM users;
This uses the regex \s+
to match any whitespace and split on it.
So in summary, SPLIT
and REGEXP_SPLIT
provide powerful tools in ksqlDB to break strings apart for analysis and filtering. Using arrays and regular expressions, you can target custom delimiters.