闲话少叙,直接开干!
先看Demo:
一、行转列
首先,我们需要收集数据,创建一个文件,内容如下:
一、启动Hive,创建表并导入数据:
1.1 创建表
create table test(name string,xingzuo string,xuexing string)
row format delimited fields delimated by "\t";
1.2 导入数据
load data local inpath "找到你所创建的文件" into table test;
1.3 行转列查询
select t1.base,concat_ws('|', collect_set(t1.name)) name
from
(select name,concat(xingzuo,",",xuexing) base
from
test) t1 group by t1.base;
1.4 查询结果
二、列转行
重复上面步骤即可:
2.1 准备数据
2.2 建表
create table test02(movie string,category array<string>)
row format delimited fields terminated by "\t"
collection items terminated by ",";
2.3 导入数据
load data local inpath "/usr/local/hive-1.2.1/testdata/test02.tsv" into table test02;
2.4 列转行查询
select movie, category_name
from
test02 lateral view explode(category) table_tmp as category_name;
2.5 结果展示
相信很多朋友心里肯定会说,这么简单,没错,就是这么简单,快去试试吧!