Hive collect_set()、collect_list()列转行,并对转换后的行值排序

Hive collect_set()、collect_list()列转行,和concat_ws()使用,并对转换后的行值排序

1、需求描述

对列值分组,并按一定顺序排序,最后多行合并一行,合并值左到右逆序排列。

2、考点:

  • sort_array(e: column, asc: boolean)将array中元素排序(自然排序),默认asc为true,即默认排升序
  • collect_set() 和 collect_list()的区别是前者去重,后者不去重

3.1、直接上collect_list()代码实现:

sql 复制代码
select st_name
      ,concat_ws(",",sort_array(collect_list(class),false)) 
      ,concat_ws(",",sort_array(collect_list(class),true))
      ,concat_ws(",",sort_array(collect_list(class))) 
from
(
  select "jack" as st_name, '3' as class
  union all
  select "jack" as st_name, '1' as class
  union all
  select "jack" as st_name, '2' as class
  union all
  select "jack" as st_name, '3' as class
  union all
  select "jack" as st_name, '5' as class
)tb_mid
group by st_name;

结果如下:

st_name concat_ws(,, sort_array(collect_list(class), false)) concat_ws(,, sort_array(collect_list(class), true)) concat_ws(,, sort_array(collect_list(class), true))

jack 5,3,3,2,1 1,2,3,3,5 1,2,3,3,5

Time taken: 0.16 seconds, Fetched 1 row(s)

3.2、直接上collect_set()代码实现:

sql 复制代码
select st_name
      ,concat_ws(",",sort_array(collect_set(class),false)) 
      ,concat_ws(",",sort_array(collect_set(class),true))
      ,concat_ws(",",sort_array(collect_set(class))) 
from
(
  select "jack" as st_name, '3' as class
  union all
  select "jack" as st_name, '1' as class
  union all
  select "jack" as st_name, '2' as class
  union all
  select "jack" as st_name, '3' as class
  union all
  select "jack" as st_name, '5' as class
)tb_mid
group by st_name;

结果如下:

st_name concat_ws(,, sort_array(collect_set(class), false)) concat_ws(,, sort_array(collect_set(class), true)) concat_ws(,, sort_array(collect_set(class), true))

jack 5,3,2,1 1,2,3,5 1,2,3,5

Time taken: 0.152 seconds, Fetched 1 row(s)

相关推荐
skywalk81637 小时前
lightharness第一次大规模开hive任务卡成功
人工智能·hive·agent·deepseek·lightharness
崖边看雾1 天前
Hive 手册:建表、分区、查询与调优实战指南
数据仓库·hive·hadoop
AllData公司负责人2 天前
实战二:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 Http 接口同步至 Doris,API 业务数据自动拉取入数仓
大数据·数据库·数据仓库·人工智能·算法·http·doris
CoderYanger4 天前
Java EE 进阶:11. SpringBoot 统一功能处理(第二弹):DispatcherServlet 源码分析+适配器模式
java·数据库·hive·spring boot·面试·java-ee·mybatis
AI源码8 天前
基于 Hadoop 和 Hive 的旅游景区数据的分析与可视化系统
hive·hadoop·旅游
DataY轻量数据平台10 天前
任务调度的三种模式:什么时候跑、按什么顺序跑、等谁跑完
数据仓库·etl
宽海智能仓储物流10 天前
宽海智能WCS系统常见问题全解析:从选型对接、运维定制到降本增效
运维·数据仓库·自动化
吾AI科技13 天前
基于Tez引擎的 Hive SQL 性能优化 —— 为什么脚本运行越来越慢
大数据·hive·性能优化·tez
上海潭龙东海科技有限公司14 天前
客运站管控之动态孪生技术方案(新增一屏管控模块
数据仓库·人工智能·算法·重构·动态规划
freejackman18 天前
【工程化】从“单兵作战”到“多引擎架构”:基于门面模式的重构实战
大数据·数据库·hive·sql·测试工具·设计模式·架构