1. 程式人生 > >hive一行資料中一列拆分成多行

hive一行資料中一列拆分成多行

lateral view用於和split、explode等UDTF一起使用的,能將一行資料拆分成多行資料,在此基礎上可以對拆分的資料進行聚合,lateral view首先為原始表的每行呼叫UDTF,UDTF會把一行拆分成一行或者多行,lateral view在把結果組合,產生一個支援別名表的虛擬表。

單個LATERAL VIEW:

源表(table1)資料{A:string B:array<BIGINT> C:string}

A                         B                                C

190     [1030,1031,1032,1033,1190]      select id
191     [1030,1031,1032,1033,1190]      select id

希望的結果是:

190    1030  select id

190    1031  select id

190    1032  select id

190    1033  select id

190    1190  select id

191    1030  select id

191    1031  select id

191    1032  select id

191    1033  select id

191    1190  select id

故使用select A,B,C from table_1 LATERAL VIEW explode(B) table1 as B得到上述結果

多個LATERAL VIEW的介紹:

LATERAL VIEW clauses are applied in the order that they appear. For example with the following base table:

Array<int> col1

Array<string> col2

[1, 2]

[a", "b", "c"]

[3, 4]

[d", "e", "f"]

SELECT myCol1, myCol2 FROM baseTable LATERAL VIEW explode(col1) myTable1 
AS myCol1
LATERAL VIEW explode(col2) myTable2 AS myCol2; 結果如下:

int myCol1

string myCol2

1

"a"

1

"b"

1

"c"

2

"a"

2

"b"

2

"c"

3

"d"

3

"e"

3

"f"

4

"d"

4

"e"

4

"f"

 複雜方式:

select * from tb_split;

20141018  aa|bb 7|9|0|3

20141019  cc|dd  6|1|8|5

使用方式:select datenu,des,type from tb_split 

lateral view explode(split(des,"//|")) tb1 as des

lateral view explode(split(type,"//|")) tb2 as type

執行過程是先執行from到 as cloumn的列過程,在執行select 和where後邊的語句;