select pcgid as gid,str_to_map(concat_ws(",",collect_set(concat_ws(':', mobilegid, cast(value as string)) ))) as gids,count(1) as num
from result_pair group by pcgid;
group by a的时候,如何把字段b,c变成一个map,有个str_to_map的函数,可以先把b,c连接成为一个字符串,在把整个set的结果连接成一个字符串,最后再用str_to_map
concat_ws
desc function concat_ws;
concat_ws(separator, [string | array(string)]+) - returns the concatenation of the strings separated by the separator.
collect_set
hive> desc function collect_set;
collect_set(x) - Returns a set of objects with duplicate elements eliminated
str_to_map:
hive> desc function str_to_map;
str_to_map(text, delimiter1, delimiter2) - Creates a map by parsing text
hive> select str_to_map('a:b,d:c',',',':') ;
{"a":"b","d":"c"}
hive> select str_to_map('a:b,d:c') ;
{"a":"b","d":"c"}
string to map
于 2023-05-24 15:26:47 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)