createtable row_to_col( name string, constellation string, blood_type string) row format delimited fields terminated by "," location '/data/hive_test/row_to_col';
1.3 执行转换
1 2 3
select t.base, concat_ws('|', collect_set(t.name)) from ( select name, concat(constellation, ",", blood_type) as base from row_to_col) t groupby t.base;
2、列转行 (对某列拆分,一列拆多行)
使用函数:lateral view explode(split(column, ‘,’)) num
2.1、构建测试数据
vi col_to_row.txt
1 2 3
疑犯追踪|悬疑,动作,科幻,剧情 Lie to me|悬疑,警匪,动作,心理,剧情 战狼2|战争,动作,灾难
createtable col_to_row( movie string, category array<string>) row format delimited fields terminated by "|" collection items terminated by "," location '/data/hive_test/col_to_row';
2.3、执行转换
1 2 3 4 5
select movie, category_name from col_to_row lateralview explode(category) table_tmp as category_name;