【hive】列转行—collect_set()/collect_list()/concat_ws()函数的使用场景

文章目录


一、collect_set()/collect_list():

在 Hive 中想实现按某字段分组,对另外字段进行合并,可通过collect_list()或者collect_set()实现。

  • collect_set()函数与collect_list()函数:列转行专用函数,都是将分组中的某列转为一个数组返回。有时为了字段拼接效果,多和concat_ws()函数连用。

  • collect_set()与collect_list()的区别:

collect_list()函数 - - 不去重
collect_set()函数 - - 去重

二、实际运用

1、创建测试表及插入数据 :

sql 复制代码
drop table test_1;
create table test_1(
id string,
cur_day string,
rule string
) 
row format delimited fields terminated by ',';

insert into test_1 values
('a','20230809','501'),('a','20230811','502'),('a','20230812','503'),('a','20230812','501'),('a','20230813','512'),('b','20230809','511'),('b','20230811','512'),('b','20230812','513'),('b','20230812','511'),('b','20230813','512'),('b','20230809','511'),('c','20230811','512'),('c','20230812','513'),('c','20230812','511'),('c','20230813','512');

把同一分组的不同行的数据聚合成一个行

举例1:按照id,cur_day分组,取出每个id对应的所有rule(不去重)。

sql 复制代码
select id,cur_day,collect_list(rule) as rule_total  from test_1 group by id,cur_day order by id,cur_day;

举例2:按照id,cur_day分组,取出每个id对应的所有rule(去重)。

sql 复制代码
select id,cur_day,collect_set(rule) as rule_total from test_1 group by id,cur_day order by id,cur_day;
  • 用下标可以随机取某一个
sql 复制代码
select id,cur_day,collect_list(rule)[0] as rule_one from test_1 group by id,cur_day order by id,cur_day;

select id,cur_day,collect_set(rule)[0] as rule_one from test_1 group by id,cur_day order by id,cur_day;
  • 聚合后的中的值用'|'分隔开
sql 复制代码
select id,cur_day,concat_ws('|',collect_list(rule)) as rule_total from test_1 group by id,cur_day order by id,cur_day;

select id,cur_day,concat_ws('|',collect_set(rule)) as rule_total from test_1 group by id,cur_day order by id,cur_day;
  • 例子
  • spark-sql : COLLECT_LIST里边字段起别名.(as等其他方式都用过,都报错.
    最后用子查询来解决)
sql 复制代码
SELECT fenceCode,
       COLLECT_LIST(STRUCT(vehicleNo, plateColor, enterTime, levaeTime, trans)) AS actInfos
FROM (
    SELECT fence_code AS fenceCode,
           veh_no AS vehicleNo,
           veh_color AS plateColor,
           enter_time AS enterTime,
           out_time AS levaeTime,
           trans
    FROM mid.ct_fence_into_out_dt where dt = 20230911  
) subquery
GROUP BY fenceCode;

总结

如果此篇文章有帮助到您, 希望打大佬们能关注点赞收藏评论支持一波,非常感谢大家!

如果有不对的地方请指正!!!

参考1

相关推荐
weixin_307779139 小时前
在 Microsoft Azure 上部署 ClickHouse 数据仓库:托管服务与自行部署的全面指南
开发语言·数据库·数据仓库·云计算·azure
weixin_3077791313 小时前
AWS Redshift 数据仓库完整配置与自动化管理指南
开发语言·数据仓库·python·云计算·aws
yumgpkpm15 小时前
CMP (类ClouderaCDP7.3(404次编译) )华为鲲鹏Aarch64(ARM)信创环境多个mysql数据库汇聚的操作指南
大数据·hive·hadoop·zookeeper·big data·cloudera
华阙之梦16 小时前
【在 Windows 上运行 Apache Hadoop 或 Spark/GeoTrellis 涉及 HDFS 】
hadoop·windows·apache
还是大剑师兰特21 小时前
Hadoop面试题及详细答案 110题 (96-105)-- Hadoop性能优化
hadoop·大剑师·hadoop面试题
ApacheSeaTunnel2 天前
新兴数据湖仓手册·从分层架构到数据湖仓架构(2025):数据仓库分层的概念与设计
大数据·数据仓库·开源·数据湖·dataops·白鲸开源·底层技术
洛克大航海2 天前
Ubuntu中使用Hadoop的HDFS和MapReduce
hadoop·ubuntu·hdfs·mapreduce
夫唯不争,故无尤也2 天前
Maven创建Java项目实战全流程
java·数据仓库·hive·hadoop·maven
想ai抽2 天前
深入starrocks-怎样实现多列联合统计信息
java·数据库·数据仓库
还是大剑师兰特2 天前
Hadoop面试题及详细答案 110题 (71-85)-- 集群部署与运维
大数据·hadoop·大剑师·hadoop面试题