HiveQL(配置单元查询语言):将类似矩阵的表转换为3列表
我的表格结构如下:HiveQL(配置单元查询语言):将类似矩阵的表转换为3列表,hive,hiveql,Hive,Hiveql,我的表格结构如下: code | col1 | col2 | col3 | 0001 | c11 | c12 | c13 | 0002 | c21 | c22 | c23 | 0003 | c31 | c32 | c33 | 0004 | c41 | c42 | c43 | 我想把它转换成 code | col_to_row | value 0001 | col1 | c11 0001 | col2 | c12
code | col1 | col2 | col3 |
0001 | c11 | c12 | c13 |
0002 | c21 | c22 | c23 |
0003 | c31 | c32 | c33 |
0004 | c41 | c42 | c43 |
我想把它转换成
code | col_to_row | value
0001 | col1 | c11
0001 | col2 | c12
0001 | col3 | c13
0002 | col1 | c21
还有一个。是否有任何内置函数用于此操作,因为我不希望使用下面的查询,因为我的输入表中的列可以随时间增长
select code,'col1' as col_to_row, col1 as value, from database.table
union all
select code,'col2' as col_to_row, col2 as value, from database.table
union all
select code,'col3' as col_to_row, col3 as value, from database.table
您可以将地图功能与
横向视图分解
一起使用,作为比联合所有
更易于修改的替代方法
select code, col_to_row, value
from (
select code, map( "col1", col1
, "col2", col2
, "col3", col3
) as col_map
from t ) x
lateral view explode(col_map) exptab as col_to_row, value ;
样品O/p
Total MapReduce CPU Time Spent: 3 seconds 620 msec
OK
1 col2 c12
1 col1 c11
1 col3 c13
2 col2 c22
2 col1 c21
2 col3 c23
3 col2 c32
3 col1 c31
3 col3 c33
4 col2 c42
4 col1 c41
4 col3 c43
Time taken: 27.876 seconds, Fetched: 12 row(s)
使用的配置单元版本是什么?@stack0114106:hive 0.13.1请检查我的答案,如果它对您有效,请接受/更新投票,这样它也会帮助其他寻求答案的人。请阅读: