Hive_语法_行列转换

用Hive处理数据的时候经常会遇到行列互相转换的需求,总结并记录一下行列转行的常见场景和操作语法
其中所有的操作都可以直接复制语句去自己的hive执行,查看结果

前置知识

执行hive或beeline进入,执行desc function explode;查看函数说明;

explode(a) - separates the elements of array a into multiple rows, or the elements of a map into multiple rows and columns
	可以将数组炸开成多行,或者将map炸开成多行多列,是Hive内置的UDTF

split(str, regex) - Splits str around occurances that match regex
	按照正则规则去切割字符串
    
collect_list(x) - Returns a list of objects with duplicates
    返回不去重的集合
    
collect_set(x) - Returns a set of objects with duplicate elements eliminated
	返回一个去重的集合

concat_ws(separator, [string | array(string)]+) - returns the concatenation of the strings separated by the separator
	返回一个特定分隔符的拼接字符串

max(expr) - Returns the maximum value of expr
	返回表达式的最大值

准备数据

创建一个name,subject,score的期末考试成绩表,代表每个同学每个学科的成绩
导入数据也可以自定义文本然后load也行,sql语句一步到位,测试简单方便

  • 用于行专列的数据,后称表1
create table school_final_test as
select 'jack' as name, 'english' as subject, 70 as score union all
select 'jack' as name, 'math' as subject, 80 as score union all
select 'jack' as name, 'chinese' as subject, 90 as score union all
select 'tim' as name, 'english' as subject, 10 as score union all
select 'tim' as name, 'math' as subject, 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值