Hive查询嵌套数组

时间:2014-02-21 15:19:55

标签: hadoop hive etl

我有一个记录购物车信息的字段,如下所示。数组的长度不固定。

[["SKU_001","Product Name A","Category1",$10.5,1],
["SKU002","Product Name B","Category1",$20,3]]

我的gola是将它反正规化为一个扁平的行,看起来像这样:

SKU    | Product Name   | Category  | Unit Price | Product Quantity

SKU001 | Product Name A | Category1 |  $10.5     |   1

SKU002 | Product Name B | Category2 |  $20       |   3

是否可以在不编写UDF的情况下实现转换?

1 个答案:

答案 0 :(得分:0)

您可以使用“lateral view explode(...)”语法将子数组作为行,然后选择子数组的各个元素以将它们作为列。

select
  cart[0] as SKU,
  cart[1] as product_name,
  cart[2] as category,
  cart[3] as unit_price,
  cart[4] as product_quantity
from my_table lateral view explode(my_nested_array_field) t as cart