多行轉(zhuǎn)一行(collect_set(col)):
- 原始數(shù)據(jù)

Paste_Image.png
hive>create table tmp_wlt_test1 as select id,collect_set(name) as name_set from tmp_wlt_test group by id;

Paste_Image.png
hive> select id,name_set[1] from tmp_wlt_test1;

Paste_Image.png
注:可以利用concat_ws將array<string>類型的數(shù)據(jù)轉(zhuǎn)化為string類型,可以利用split將string轉(zhuǎn)為array<string>, array的起始數(shù)字為0
hive> create table tmp_wlt_test2 as select id,concat_ws(',',collect_set(name)) as name_set from tmp_wlt_test group by id;

Paste_Image.png

Paste_Image.png
一行轉(zhuǎn)多行(explode(ARRAY)):

Paste_Image.png
里面name_set是一個array<string>類型的數(shù)據(jù),這里報錯的原因是因為有id字段,當select中只有name_set一列數(shù)據(jù)時,可以跑通
hive> create table tmp_wlt_test3 as select explode(name_set) as name from tmp_wlt_test1;

Paste_Image.png
一行轉(zhuǎn)多行(explode(ARRAY), Lateral View ):
lateral view 可以解決explode無法添加額外的select列的問題。
Lateral view 其實就是用來和像類似explode這種UDTF函數(shù)聯(lián)用的。lateral view 會將UDTF生成的結(jié)果放到一個虛擬表中,然后這個虛擬表會和輸入行即每個game_id進行join 來達到連接UDTF外的select字段的目的。
lateral view用法:Lateral View用法 與 Hive UDTF explode - OopsOutOfMemory盛利的博客 - 博客頻道 - CSDN.NET
上述問題可以用如下語句解決:
hive> create table tmp_wlt_test3 as select a.id,b.name from tmp_wlt_test1 a lateral view explode(name_set) b as name; -- lateral view explode(name_set) 臨時表名 as 列名

Paste_Image.png