Hive--行转列(Lateral View explode())和列转行(collect_set() 去重)

 

一行转多行

说明:lateral view用于和split、explode等UDTF一起使用的,能将一行数据拆分成多行数据,在此基础上可以对拆分的数据进行聚合,lateral view首先为原始表的每行调用UDTF,UDTF会把一行拆分成一行或者多行,lateral view在把结果组合,产生一个支持别名表的虚拟表。

 

1.行转列

1.1 问题引入:

如何将

a       b       1,2,3

c       d       4,5,6

变为:

a       b       1

a       b       2

a       b       3

c       d       4

c       d       5

c       d       6 

1.2 原始数据:

test.txt

a b 1,2,3

c d 4,5,6

1.3 解决方法

方案1:

drop table test_jzl_20140701_test;

建表:

create table test_jzl_20140701_test

(

col1 string,

col2 string,

col3 string

)

row format delimited fields terminated by ' '

stored as textfile;

加载数据:

load data local inpath '/home/jiangzl/shell/test.txt' into table test_jzl_20140701_test;

查看表中所有数据:

select * from test_jzl_20140701_test  

a       b       1,2,3

c       d       4,5,6

遍历数组中的每一列

select col1,col2,name 

from test_jzl_20140701_test  

lateral view explode(split(col3,',')) col3 as name;

a       b       1

a       b       2

a       b       3

c       d       4

c       d       5

c       d       6 

方案2:

drop table test_jzl_20140701_test1;

建表:

create table test_jzl_20140701_test1

(

col1 string,

col2 string,

col3 array<int>

)

row format delimited 

fields terminated by ' '

collection items terminated by ','   //定义数组的分隔符

stored as textfile;

加载数据:

load data local inpath '/home/jiangzl/shell/test.txt' into table test_jzl_20140701_test1;

查看表中所有数据:

select * from test_jzl_20140701_test1; 

a       b       [1,2,3]

c       d       [4,5,6]

遍历数组中的每一列:

select col1,col2,name 

from test_jzl_20140701_test1  

lateral view explode(col3) col3 as name;

a       b       1

a       b       2

a       b       3

c       d       4

c       d       5

c       d       6

1.4补充知识点:

select * from test_jzl_20140701_test; 

a       b       1,2,3

c       d       4,5,6

 

select t.list[0],t.list[1],t.list[2] from (

select (split(col3,',')) list from test_jzl_20140701_test)t;

 

OK

1       2       3

4       5       6

 

--查看数组长度

select size(split(col3,',')) list from test_jzl_20140701_test;

3

3

 

2.列转行

2.1问题引入:

hive如何将

a       b       1
a       b       2
a       b       3
c       d       4
c       d       5
c       d       6

变为:

a       b       1,2,3
c       d       4,5,6

2,2原始数据:

test.txt

a       b       1
a       b       2
a       b       3
c       d       4
c       d       5
c       d       6

2.3 解决方法:

drop table tmp_jiangzl_test;

建表:

create table tmp_jiangzl_test
(
col1 string,
col2 string,
col3 string
)
row format delimited fields terminated by '\t'
stored as textfile;
加载数据:
load data local inpath '/home/jiangzl/shell/test.txt' into table tmp_jiangzl_test;

处理:

select col1,col2,concat_ws(',',collect_set(col3))
from tmp_jiangzl_test  
group by col1,col2;


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive中的列转行行转列是通过使用一些特定的内置函数和关键字来实现的。列转行可以使用EXPLODE函数,该函数可以将一个包含复杂结构的数组或者映射拆分成多行。行转列可以使用collect_set函数,该函数将某一列的所有数据转化为一个集合,并且可以使用concat_ws函数将集合中的所有元素以逗号分割连接成一个字符串。此外,为了使用EXPLODELATERAL VIEW函数,你可以使用LATERAL VIEW关键字,语法为LATERAL VIEW udtf(expression) tableAlias AS columnAlias。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [hive列转行案例](https://download.csdn.net/download/weixin_38581777/14037437)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [hive行转列列转行](https://blog.csdn.net/qq_24790473/article/details/109710145)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [hive操作(行转列列转行)](https://blog.csdn.net/aiduo3346/article/details/102085019)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值