hive查询嵌套数组

pxy2qtax  于 2021-06-03  发布在  Hadoop
关注(0)|答案(1)|浏览(431)

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

[["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

不写自定义项就可以实现转换吗?

6g8kf2rb

6g8kf2rb1#

您可以使用“横向视图分解(…)”语法将子数组获取为行,然后选择子数组的各个元素以将它们获取为列。

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

相关问题