Hive Explode / Lateral View multiple arrays

后端 未结 5 944
刺人心
刺人心 2020-11-30 02:23

I have a hive table with the following schema:

COOKIE  | PRODUCT_ID | CAT_ID |    QTY    
1234123   [1,2,3]    [r,t,null]  [2,1,null]

How

5条回答
  •  感动是毒
    2020-11-30 02:31

    You can do this by using posexplode, which will provide an integer between 0 and n to indicate the position in the array for each element in the array. Then use this integer - call it pos (for position) to get the matching values in other arrays, using block notation, like this:

    select 
      cookie, 
      n.pos as position, 
      n.prd_id as product_id,
      cat_id[pos] as catalog_id,
      qty[pos] as qty
    from table
    lateral view posexplode(product_id_arr) n as pos, prd_id;
    

    This avoids the using imported UDF's as well as joining various arrays together (this has much better performance).

提交回复
热议问题