Hive 行列转换

Hive 行列转换的sql写法

列转行

使用函数:lateral view explode(split(column, ‘,’)) num
eg: 如表:t_row_to_column_tmp 数据如下,对tag列进行拆分

1
2
3
4
select id,tag,tag_new
from t_row_to_column_tmp
lateral view explode(split(tag, ',')) num as tag_new
where id=212022894;


行转列

使用函数:concat_ws(‘,’,collect_set(column))
说明:collect_list 不去重,collect_set 去重。 column 的数据类型要求是 string
eg:如表:t_column_to_row ,根据id,对tag_new 进行合并

1
2
3
4
select id,
concat_ws(',',collect_set(tag_new)) as tag_col
from t_column_to_row
group by id;

1
2
3
4
select id,
concat_ws(',',collect_list(tag_new)) as tag_col
from t_column_to_row
group by id;