Search code examples
sqlarraysstructhivehiveql

Explode an array of structs with element position in HIVE


Does anybody know if there is something similar to lateral view posexplode but with array of struct? I want to explode an array of structs to multiple rows while computing the position of the element. I use lateral view inline to explode but it does not give element position.

For example in data I have smth like: [{"group":A,"score":20},{"group":B,"score":30}]

I would like to see the table:

nb group score
1 A 20
2 B 30

Solution

  • Use lateral view posexplode, then access struct fields using dot:

    select t.*,
           e.pos, --position
           --struct fields
           e.st.group,
           e.st.score 
      from table_name t 
           lateral view outer posexplode (t.my_array_of_struct) e as pos, st