If you want to convert a string into a map for query and report generation, you can use the STR_TO_MAP
function of MaxCompute. This function splits a string into key-value pairs by using delimiter1 and then separates keys from values in the key-value pairs by using delimiter2. This topic describes how to use the STR_TO_MAP function to convert a string into a map in MaxCompute.
Syntax
str_to_map([string <mapDupKeyPolicy>,] <text> [, <delimiter1> [, <delimiter2>]])
Parameters
mapDupKeyPolicy: optional. A value of the STRING type. This parameter specifies the method that is used to process duplicate keys. Valid values:
exception: An error is returned.
last_win: The latter key overwrites the former key.
NoteThe behavior implementation of MaxCompute is determined based on mapDupKeyPolicy. If you do not specify mapDupKeyPolicy, the value of
odps.sql.map.key.dedup.policy
is used.text: required. This parameter specifies the string that you want to split, which is of the STRING type.
delimiter1: optional. This parameter specifies a delimiter of the STRING type. If no value is specified for this parameter, a comma (
,
) is used.delimiter2: optional. This parameter specifies a delimiter of the STRING type. If no value is specified for this parameter, an equal sign (
=
) is used.NoteIf the delimiter is a regular expression or special character, you must add two backslashes (\) before the delimiter for escaping. The following special characters can be used as a delimiter: . ? + * :
Return value
A value of the MAP<STRING, STRING>
type is returned. The return value indicates that the string specified by text is split by using delimiter1 and delimiter2.
Examples
-- The return value is {test1:1, test2:2}.
SELECT str_to_map('test1&1-test2&2','-','&');
-- The return value is {test1:1, test2:2}.
SELECT str_to_map("test1.1,test2.2", ",", "\\.");
-- The return value is {test1:1, test2:3}.
SELECT str_to_map("test1.1,test2.2,test2.3", ",", "\\.");
Related functions
For more information, see Other functions.