Hive 集合函数 collect_set() collect_list()

集合函数 collect_set() collect_list()

实验数据1

userid username
11101 张三
11101 李四
11101 王五
11101 赵六
11101 张三

注意到张三出现了两次

-- 建表语句
create table temp.strategy_temp_20200813_function_test (
    userid string,
    username string
)row format delimited fields terminated by ',' STORED AS TEXTFILE

-- 查看
select * from temp.strategy_temp_20200813_function_test t

collect_set()

collect_set() 通常用于列转行,将某一个列转换成为一行且去重。

-- 去重的合并
select userid, collect_set(username) username
from temp.strategy_temp_20200813_function_test t
group by userid

结果是

user_id username
11101 ["张三","李四",”王五","赵六"]

若要不去重,则需要使用 collect_list(), 若需要对合并内容排序则使用 group_concat()

collect_list()

collect_list() 通常用于列转行, 将某一列合并后,转换成一行,不去重。

-- 去重的合并
select userid, collect_list(username) username
from temp.strategy_temp_20200813_function_test t
group by userid

结果是

user_id username
11101 ["张三","李四",”王五","赵六","张三"]
posted @ 2020-11-19 14:51  何大卫  阅读(3017)  评论(0编辑  收藏  举报