12、HIve数据仓库——行列转换

21 篇文章 4 订阅

HIve数据仓库——行列转换

行转列

创建表

create table testArray2(
    name string,
    weight array<string>
)row format delimited 
fields terminated by '\t'
COLLECTION ITEMS terminated by ',';

加载数据

  • 数据
志凯	"150","170","180"
上单	"150","180","190"
  • 加载数据
load data local inpath '/usr/local/soft/data/testArray2.txt' into table testArray2;

  • 使用lateral view explode函数
select name,col1 from testArray2 lateral view explode(weight) t1 as col1;
select key from (select explode(map('key1',1,'key2',2,'key3',3)) as (key,value)) t1;
select name,col1,col2 from testArray2 lateral view explode(map('key1',1,'key2',2,'key3',3)) t1 as col1,col2;
select name,pos,col1 from testArray2 lateral view posexplode(weight)  t1 as pos,col1;

列转行

创建表

create table testLieToLine(
    name string,
    col1 int
)row format delimited 
fields terminated by '\t';

加载数据

  • 数据
志凯	150
志凯	170
志凯	180
上单	150
上单	180
上单	190
  • 加载数据
load data local inpath '/usr/local/soft/data/testLieToLine.txt' into table testLieToLine;

  • 使用函数collect_list( )
select name,collect_list(col1) from testLieToLine group by name;
select  t1.name
        ,collect_list(t1.col1) 
from (
    select  name
            ,col1 
    from testarray2 
    lateral view explode(weight) t1 as col1
) t1 group by t1.name;

到底啦!关注靓仔学习更多的大数据知识!ψ(`∇´)ψ

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liangzai2048

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值