hive中实现行转列_hive中的列转行和行转列

1、列转行

1.1 相关函数的说明:

concat(string1,string,...)  //连接括号内字符串,数量不限。

concat_ws(separator,string1,string2,...)  //连接括号内字符串,数量不限,连接符为separator。

collect_set(col)  //此函数只接受基本类型,主要是将字段的值进行去重汇总,产生array类型字段。

1.2 例子:

创建表:create table person_info(

name string,

constellation string,

blood_type string

)row format delimited fields terminated by '\t';

上传数据:

load data local inpath ‘/home/hdc/constellation.txt’ into table person_info;

查询语句:

select t.base,concat_ws('|',collect_set(t.name)) name

from(

select name,concat(costellation,',',blood_type) base

from person_info

)t

group by t.base;

2、行转列

2.1 相关函数:

explode(col_name):将hive中的一列中复杂的array或者map分成多行

lateral view:侧视图配合explode(或者其他的UDTF),一个语句生成把单行数据拆解成多行后的数据结果集。  //LATERAL VIEW explode(split(goods_id,','))goods相当于一个虚拟表

2.2 例子:

创建表:create table movie_info(

name string,

categroy array

)row format delimited fields terminated by '\t';

collection items terminated by ',';

上传数据:

load data local inpath '/home/hdc/movie.txt' into table movie_info;

查询语句:

select name,category_type

from movie_info lateral view explode(categroy) temp_table as category_type;

解析:表movie_info与虚表temp_table进行笛卡尔乘积其中temp_table表中的字段为category_type

explode还有如下用法:

select distinct(t2.videoid), t3.category

from (

select explode(relatedid) as videoid

from (

select *

from video_orc

order by views desc

limit 50) t1

)t2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值