Hive 行列转换

行列转换
列转行

使用 lateral view + explode(array|map)lateral view + inline(array_struct) 可以将列转换为行。

  • 单列转多行,降维(单列数组或键值对)

示例1:explode(array(...))

sql 复制代码
select ..., A
from T
lateral view explode(ARRAY_FIELD) as A;
sql 复制代码
select explode(`array`(88.2,98.3,67.1)) AS (price);

示例2:explode(map(...))

sql 复制代码
select ..., K, V
from T
lateral view explode(MAP_FIELD) as K, V;
sql 复制代码
select explode(`map`("java",56,"mysql",88,"javascript",66)) AS (subject, score);

示例3:inline(array_struct)

sql 复制代码
select ..., 
from T
lateral view inline(STRUCT_ARRAY_FIELD)V as F1,...,FN;
sql 复制代码
with tmp as (
select array(
	named_struct('name','henry','age',22,'is_member','true'),
	named_struct('name','pola','age',20,'is_member','true'),
	named_struct('name','ariel','age',19,'is_member','true')
   ) AS array_struct
)
select name,age,is_member
from tmp
lateral view inline(array_struct)V as name,age,is_member;

lateral view inline(array_struct)将结构体数组的每个元素都转化为一行,每一行都包含结构体字段的值.

前:

后:

  • 多列转多行
sql 复制代码
select ..., A
from T
lateral view explode(array|map(F1,...,FN))V as A;

示例:

sql 复制代码
SELECT name, class, Scores.subject, Scores.score
FROM Students
LATERAL VIEW EXPLODE(ARRAY(
	named_struct('subject','math','score',math_score),
	named_struct('subject','science','score',science_score)
	)
) V AS Scores;

前:

后:

行转列
  • 多行转多列
    条件聚合,通常用于将多行数据中满足条件的某个值聚合到单个行中。
sql 复制代码
select
		F1,...,
		sum(if(C1,0,V1)) as A1,
		sum(if(C2,0,V2)) as A2,
		sum(if(C3,0,V3)) as A3
	from TABLE_NAME
	group by F1,...
	
	drop table if exists lateral_view_stack_test1w;
	create table lateral_view_stack_test1w as
	select year,
		   sum(if(month(order_time)=1,order_amount,0)) as sum_jan,
		   sum(if(month(order_time)=2,order_amount,0)) as sum_feb,
		   sum(if(month(order_time)=3,order_amount,0)) as sum_mar,
		   sum(if(month(order_time)=4,order_amount,0)) as sum_apr,
		   sum(if(month(order_time)=5,order_amount,0)) as sum_may,
		   sum(if(month(order_time)=6,order_amount,0)) as sum_jun,
		   sum(if(month(order_time)=7,order_amount,0)) as sum_jul,
		   sum(if(month(order_time)=8,order_amount,0)) as sum_aug,
		   sum(if(month(order_time)=9,order_amount,0)) as sum_sep,
		   sum(if(month(order_time)=10,order_amount,0)) as sum_oct,
		   sum(if(month(order_time)=11,order_amount,0)) as sum_nov,
		   sum(if(month(order_time)=12,order_amount,0)) as sum_dec
	from hive_internal_par_regex_test1w
	where year>=2014
	group by year;
相关推荐
AllData公司负责人1 天前
AllData数据中台通过集成开源项目Apache IOTDB Web相关项目,建设物联网数据库平台
数据仓库·物联网·时序数据库·iotdb·工业物联网·apache iotdb·物联网数据库平台
android_cai_niao1 天前
kotlin中的内联函数重载
inline·crossinline·inlineonly
Leo.yuan1 天前
数据仓库是什么?数据仓库和BI有什么区别?
数据仓库
heimeiyingwang2 天前
【架构实战】ETL架构演进:从批处理到实时流处理
数据仓库·架构·etl
素玥2 天前
实训4 ETL构建中间层
数据仓库·etl
苛子2 天前
ETL与ELT的区别与选择:企业数据集成方案深度对比
数据仓库·etl
清水白石0082 天前
Python 日志采集到数据仓库 ETL 流程设计实战:从基础语法到生产级可靠运维
数据仓库·python·etl
2501_933329552 天前
企业舆情处置系统设计与实践:Infoseek数字公关AI中台技术解析
数据仓库·人工智能·重构·架构·数据库开发
莫叫石榴姐3 天前
字节广告数开一面 | 实习
大数据·数据仓库·面试
2501_933329553 天前
AI驱动媒介宣发:Infoseek舆情系统的技术架构与公关实战
数据仓库·人工智能·重构·数据库开发