MULTI_KEYVALUE is a table function that extracts multiple values from a key-value formatted string in a single call. It splits the string into key-value pairs using the delimiter specified by split1, then splits each pair into a key and a value using the delimiter specified by split2, and returns the values for the requested keys.
Supported in Realtime Compute for Apache Flink with Ververica Runtime (VVR) 3.0.0 or later.
Syntax
MULTI_KEYVALUE(VARCHAR str, VARCHAR split1, VARCHAR split2, VARCHAR key_name1, VARCHAR key_name2, ...)Because MULTI_KEYVALUE is a table function, call it with a lateral table join:
SELECT c1, c2
FROM T1, lateral table(MULTI_KEYVALUE(str, split1, split2, key1, key2))
as T(c1, c2);Parameters
| Parameter | Type | Description |
|---|---|---|
str | VARCHAR | The string to parse. |
split1 | VARCHAR | The key-value pair delimiter. If NULL, spaces are used. If the value is longer than one character, each character is treated as a valid delimiter independently. |
split2 | VARCHAR | The key-value delimiter. If NULL, spaces are used. If the value is longer than one character, each character is treated as a valid delimiter independently. |
key_name1, key_name2, ... | VARCHAR | The keys whose values to return. Add as many key arguments as needed. |
When to use KEYVALUE instead
In VVR 8.0.7 and earlier, use the KEYVALUE scalar function instead of MULTI_KEYVALUE in either of these situations:
All values for
split1,split2, and everykey_nameargument are constant strings.Within a namespace running a specific engine version,
MULTI_KEYVALUEis called multiple times with differentsplit1andsplit2delimiters — whether in the same deployment or across multiple deployments. WhenKEYVALUEparses different key names from the same field in repeated calls, Flink reuses intermediate parsing results and applies a binary parsing mechanism, which improves overall processing efficiency. BecauseKEYVALUEis a scalar function, call it once per key.
Example
Basic usage
Parse k1=v1;k2=v2 with ; as the pair delimiter and = as the key-value delimiter:
-- Input: str = 'k1=v1;k2=v2', split1 = ';', split2 = '='
SELECT c1, c2
FROM T1, lateral table(MULTI_KEYVALUE(str, split1, split2, 'k1', 'k2'))
as T(c1, c2);
-- Output: c1 = 'v1', c2 = 'v2'Behavior reference
The following table shows how MULTI_KEYVALUE handles various delimiter combinations and edge cases. All rows use the query shown above against table T1.
Test data (table T1)
| str | split1 | split2 | key1 | key2 |
|---|---|---|---|---|
| k1=v1;k2=v2 | ; | = | k1 | k2 |
| NULL | ; | = | k1 | k2 |
| k1:v1;k2:v2 | ; | : | k1 | k3 |
| k1:v1;k2:v2 | ; | = | k1 | k2 |
| k1:v1;k2:v2 | , | : | k1 | k2 |
| k1:v1;k2=v2 | ; | : | k1 | k2 |
| k1:v1abck2:v2 | abc | : | k1 | k2 |
| k1:v1;k2=v2 | ; | := | k1 | k2 |
| k1:v1 k2:v2 | NULL | : | k1 | k2 |
| k1 v1;k2 v2 | ; | NULL | k1 | k2 |
Test statement
SELECT c1, c2
FROM T1, lateral table(MULTI_KEYVALUE(str, split1, split2, key1, key2))
as T(c1, c2);Results
| c1 | c2 |
|---|---|
| v1 | v2 |
| NULL | NULL |
| v1 | NULL |
| NULL | NULL |
| NULL | NULL |
| v1 | NULL |
| v1 | v2 |
| v1 | v2 |
| v1 | v2 |
| v1 | v2 |
Key observations from the results:
Row 2:
strisNULL— all output values areNULL.Row 3: key
k3does not exist in the string —c2isNULL.Row 4: the actual delimiter is
:butsplit2is=— no match, both outputs areNULL.Row 5: the actual pair delimiter is
;butsplit1is,— no match, both outputs areNULL.Row 6:
split2 = ':'matchesk1:v1but notk2=v2—c1 = 'v1',c2 = NULL.Row 7:
split1 = 'abc'is longer than one character, soa,b, andcare each treated as independent delimiters — splittingk1:v1abck2:v2correctly.Row 8:
split2 = ':='is longer than one character, so both:and=are valid key-value delimiters — matching bothk1:v1andk2=v2.Rows 9–10:
NULLdelimiters default to space.