Explode the Array of Struct in Hive

前端 未结 2 1172
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 10:04

This is the below Hive Table

CREATE EXTERNAL TABLE IF NOT EXISTS SampleTable
(
USER_ID BIGINT,
NEW_ITEM ARRAY

        
相关标签:
2条回答
  • 2020-12-02 10:36

    If you are on Hive 0.10 or later, you could also use inline(ARRAY<STRUCT[,STRUCT]>). It explodes an array of structs into a table.

    0 讨论(0)
  • 2020-12-02 10:40

    You need to explode only once (in conjunction with LATERAL VIEW). After exploding you can use a new column (called prod_and_ts in my example) which will be of struct type. Then, you can resolve the product_id and timestamps members of this new struct column to retrieve the desired result.

    SELECT
       user_id,
       prod_and_ts.product_id as product_id,
       prod_and_ts.timestamps as timestamps
    FROM 
       SampleTable 
       LATERAL VIEW explode(new_item) exploded_table as prod_and_ts;
    
    0 讨论(0)
提交回复
热议问题