Friday 14 August 2015

Lateral view explode() in hive


Lateral view is used in conjunction with user-defined table generating functions such as explode (). A UDTF generates zero or more output rows for each input row. A lateral view first applies the UDTF to each row of base table and then joins resulting output rows to the input rows to form a virtual table having the supplied table alias.

Input file:-

Column1          column2
One                 [1, 2, 3]
Two                 [3, 4, 5]

Create table lateral1 (column1 string, column2 array<int>) row format delimited collection items terminated by ‘,’ stored as textfile;

Now exploding the list of elements in an array using lateral view explode () functions:-

SELECT column1, columns
FROM lateral1 LATERAL VIEW explode (column2) lateralTable AS column;

The output result will be:-

One     1
One     2
One     3
Two     3
Two     4

Two     5

2 comments: