Skip to content

Hive Lateral view explode vs posexplode

Lateral view Explode

Lateral view explode, explodes the array data into multiple rows. for example, let’s say our table look like this, where Telephone is an array of string.

namephone_numberscities
AAA[“365-889-1234”, “365-887-2232”][“Hamilton”][“Burlington”]
BBB[“232-998-3232”, “878-998-2232”][“Toronto”, “Stoney Creek”]

Applying a lateral view explode on the above table will expand the both Telephone and Cities and do a cross join, your final table will look like this.

namephone_numberscities
AAA365-889-1234Hamilton
AAA365-887-2232Hamilton
AAA365-889-1234Burlington
AAA365-887-2232Burlington
BBB232-998-3232Toronto
BBB878-998-2232Toronto
BBB232-998-3232Stoney Creek
BBB878-998-2232Stoney Creek

Lateral View POSExplode

However, this is not what you probably want, if you want to map first telephone number to first city and second with second one, and that for all the records. Then you can use posexplode (positional explode)

posexplode gives you an index along with value when you expand any error, and then you can use this indexes to map values with each other as mentioned below.

select 
    name, 
    phone_number, 
    city 
from temp.test_laterla_view_posexplode
lateral view posexplode(phone_numbers) pn as pos_phone, phone_number
lateral view posexplode(cities) pn as pos_city, city 
where 
    pos_phone == pos_city

With above query you will get following results, where phone number is mapped with corresponding city.

namephone_numbercity
AAA365-889-1234Hamilton
AAA365-887-2232Burlington
BBB232-998-3232Toronto
BBB878-998-2232Stoney Creek
Published inHadoophive

3 Comments

  1. Kathir Kathir

    Nicely explained, thanks!

  2. krupali krupali

    Thanks for making it so simple to understand.

  3. Mohanpraveen Mohanpraveen

    Hi ,

    For example if I have in first AAA row only one string in a list both phone number and city but in second row you have two phone numbers and two cities like mix of both multiple and single how can we use pos explode in that case?

Leave a Reply

Your email address will not be published. Required fields are marked *