Skip to content
Advertisement

Hive Explode the Array of Struct key: value:

This is the below Hive Table

And this is the data in the above table-

Is there any way I can get the below output using HiveQL?

I tried use explode() but I get result like that:

Advertisement

Answer

Use laterral view [outer] inline to get struct elements already etracted and use conditional aggregation to get values corresponting to some keys grouped in single row, use group_by user_id.

Demo:

Result:

If you can guarantee the order of structs in array (one with status comes first always), you can address nested elements dirctly

One more approach, if you do not know the order in array, but array is of size=2, CASE expressions without explode will give better performance:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement