You can use the KEYVALUE function of MaxCompute to parse a key-value pair string stored in a data table and extract the value of the key from the key-value pair. This function splits the string that is specified by the str parameter into key-value pairs by the delimiter that is specified by split1, separates the key-value pairs by the delimiter that is specified by split2, and then returns the value of the key. This topic describes the parameters of the KEYVALUE function and provides examples on how to use this function.
Syntax
keyvalue(string <str>,[string <split1>,string <split2>,] string <key>)
keyvalue(string <str>,string <key>)
Parameters
str: required. A value of the STRING type. This parameter specifies the string that you want to split.
split1 and split2: optional. Values of the STRING type. These parameters specify the strings that are used as delimiters to split the source string. If you do not specify the two parameters, the default value of split1 is a semicolon (
;
) and the default value of split2 is a colon (:
). If a key-value pair that is obtained after the source string is split by split1 contains multiple delimiters specified by split2, the returned result is undefined.key: required. A value of the STRING type. After the source string is split by split1 and split2 in sequence, the value that corresponds to key is returned.
Return value
A value of the STRING type is returned. The return value varies based on the following rules:
If the value of split1 or split2 is null, null is returned.
If the value of str or key is null or no key matches, null is returned.
If multiple key-value pairs match, the value that corresponds to the first matched key is returned.
Examples
Example 1: Split the string
0:1\;1:2
into key-value pairs and return the value that corresponds to the key1
. Sample statements:-- The return value is 2. select keyvalue('0:1\;1:2', 1);
The split1 and split2 parameters are not specified. The default value of split1 is a semicolon (
;
) and the default value of split2 is a colon (:
).After the source string is split by split1, the key-value pairs
0:1\,1:2
are returned. After the key-value pairs are split by split2, the following keys and values are generated:0 1/ 1 2
The value 2 that corresponds to key 1 is returned.
Example 2: Split the string
\;decreaseStore:1\;xcard:1\;isB2C:1\;tf:21910\;cart:1\;shipping:2\;pf:0\;market:shoes\;instPayAmount:0\;
into key-value pairs based on a backslash and a semicolon (\;
), separate the values from keys based on a colon (:
), and then return the value that corresponds to the keytf
. Sample statements:-- The return value is 21910. select keyvalue("\;decreaseStore:1\;xcard:1\;isB2C:1\;tf:21910\;cart:1\;shipping:2\;pf:0\;market:shoes\;instPayAmount:0\;","\;",":","tf");
After the source string
\;decreaseStore:1\;xcard:1\;isB2C:1\;tf:21910\;cart:1\;shipping:2\;pf:0\;market:shoes\;instPayAmount:0\;
is split based on a backslash and a semicolon (\;
), the following key-value pairs are generated:decreaseStore:1, xcard:1, isB2C:1, tf:21910, cart:1, shipping:2, pf:0, market:shoes, instPayAmount:0
After the key-value pairs are separated based on a colon (
:
), the following keys and values are generated:decreaseStore 1 xcard 1 isB2C 1 tf 21910 cart 1 shipping 2 pf 0 market shoes instPayAmount 0
The value 21910 that corresponds to the key
tf
is returned.
Related functions
KEYVALUE is a string function. For more information about functions related to string searches and conversion, see String functions.