如何 select hive sql 中的特定结构值数组?
How to select an array of specific struct values in hive sql?
假设我在名为 myTable:
的 table 中有一列包含这种格式的数据
myColumn
[{"id": 1, color: "red"}, {"id": 2, color: "blue"}]
[{"id": 1, color: "orange"}, {"id": 2, color: "purple"}]
如何以这种格式将每行的颜色提取到数组中?
result
[red, blue]
[orange, purple]
到目前为止我尝试了什么 -
select arr.color
from myTable as mt
lateral view outer explode(mt.myColumn) as arr
limit 10;
不幸的是,这会产生每行包含 1 种颜色的结果。如何为每行中的颜色创建一个数组?
您不需要使用 explode
功能。您可以直接在数组列上调用所需的字段:
SELECT myColumn.color FROM myTable
假设我在名为 myTable:
的 table 中有一列包含这种格式的数据myColumn |
---|
[{"id": 1, color: "red"}, {"id": 2, color: "blue"}] |
[{"id": 1, color: "orange"}, {"id": 2, color: "purple"}] |
如何以这种格式将每行的颜色提取到数组中?
result |
---|
[red, blue] |
[orange, purple] |
到目前为止我尝试了什么 -
select arr.color
from myTable as mt
lateral view outer explode(mt.myColumn) as arr
limit 10;
不幸的是,这会产生每行包含 1 种颜色的结果。如何为每行中的颜色创建一个数组?
您不需要使用 explode
功能。您可以直接在数组列上调用所需的字段:
SELECT myColumn.color FROM myTable