Hive典型应用场景之行列转换
2017-08-30 02:24
225 查看
在使用Hive处理数据时,经常遇到行列转换的场景,本文将对Hive的行列转换操作做详细的说明。
假设数据表
row2col:
现在要将其转化为:
此时需要使用到max(case … when … then … else 0 end),仅限于转化的字段为数值类型,且为正值的情况。
HQL语句为:
2)多行转单列
假设数据表
row2col:
现在要将其转化为:
此时需要用到两个内置的UDF:
a)cocat_ws(参数1,参数2),用于进行字符的拼接
参数1—指定分隔符
参数2—拼接的内容
b)collect_set(),它的主要作用是将某字段的值进行去重汇总,产生array类型字段。
HQL语句为:
注意:由于使用concat_ws()函数,collect_set()中的字段必须为string类型,如果是其他类型可使用cast(col3 as string)将其转换为string类型。
假设有数据表
col2row:
现要将其转化为:
这里需要使用union进行拼接。
HQL语句为:
2)单列转多行
假设有数据表
col2row:
现要将其转化为:
这里需要使用UDTF(表生成函数)explode(),该函数接受array类型的参数,其作用恰好与collect_set相反,实现将array类型数据行转列。explode配合lateral view实现将某列数据拆分成多行。
HQL语句为:
行转列
1)多行转多列假设数据表
row2col:
col1 col2 col3 a c 1 a d 2 a e 3 b c 4 b d 5 b e 6
现在要将其转化为:
col1 c d e a 1 2 3 b 4 5 6
此时需要使用到max(case … when … then … else 0 end),仅限于转化的字段为数值类型,且为正值的情况。
HQL语句为:
select col1, max(case col2 when 'c' then col3 else 0 end) as c, max(case col2 when 'd' then col3 else 0 end) as d, max(case col2 when 'e' then col3 else 0 end) as e from row2col group by col1;
2)多行转单列
假设数据表
row2col:
col1 col2 col3 a b 1 a b 2 a b 3 c d 4 c d 5 c d 6
现在要将其转化为:
col1 col2 col3 a b 1,2,3 c d 4,5,6
此时需要用到两个内置的UDF:
a)cocat_ws(参数1,参数2),用于进行字符的拼接
参数1—指定分隔符
参数2—拼接的内容
b)collect_set(),它的主要作用是将某字段的值进行去重汇总,产生array类型字段。
HQL语句为:
select col1, col2, concat_ws(',', collect_set(col3)) as col3 from row2col group by col1, col2;
注意:由于使用concat_ws()函数,collect_set()中的字段必须为string类型,如果是其他类型可使用cast(col3 as string)将其转换为string类型。
列转行
1)多列转多行假设有数据表
col2row:
col1 c d e a 1 2 3 b 4 5 6
现要将其转化为:
col1 col2 col3 a c 1 a d 2 a e 3 b c 4 b d 5 b e 6
这里需要使用union进行拼接。
HQL语句为:
select col1, 'c' as col2, c as col3 from col2row UNION select col1, 'd' as col2, d as col3 from col2row UNION select col1, 'e' as col2, e as col3 from col2row order by col1, col2;
2)单列转多行
假设有数据表
col2row:
col1 col2 col3 a b 1,2,3 c d 4,5,6
现要将其转化为:
col1 col2 col3 a b 1 a b 2 a b 3 c d 4 c d 5 c d 6
这里需要使用UDTF(表生成函数)explode(),该函数接受array类型的参数,其作用恰好与collect_set相反,实现将array类型数据行转列。explode配合lateral view实现将某列数据拆分成多行。
HQL语句为:
select col1, col2, lv.col3 as col3 from col2row lateral view explode(split(col3, ',')) lv as col3;
相关文章推荐
- ZooKeeper的典型应用场景之集群管理。
- Oracle SQL行列转换应用实例一则
- ZooKeeper典型应用场景
- ZooKeeper典型应用场景一览
- ZooKeeper典型应用场景(转)
- ZooKeeper典型应用场景一览
- ZooKeeper 典型的应用场景——及编程实现
- 细数阿里云服务器的十二种典型应用场景
- ZooKeeper典型应用场景
- ZooKeeper 典型应用场景一览
- hbase+hive应用场景
- 【Zookeeper系列二】ZooKeeper典型应用场景实践
- ZooKeeper 的典型应用场景
- Oracle一个典型行列转换的几种实现方法(转)
- ZooKeeper典型应用场景
- ZooKeeper典型应用场景一览
- ZooKeeper典型应用场景(转)
- hive 窗口函数 应用场景与实战详解
- ZooKeeper 典型应用场景一览
- SQL语句行列转换两种方法 case ...when 和pivot函数应用