问题描述
我在 Hive 表中有一个列,类型为:
array<array<struct<type:string,value:string,currency:string>>>
这是列中的数据示例:
[
[
{
"type": "PROFIT","value": "100","currency": "USD"
},{
"type": "NET","value": "50",{
"type": "TOTAL","value": "250","currency": "USD"
}
]
]
如何查询列的每个“类型”?
利润 | NET | 总计 |
---|
解决方法
分解上层数组获取struct的内部数组,使用数组索引[]访问struct,使用点访问struct元素:
select a1.inner_array[0].type as type1,a1.inner_array[1].type as type2,a1.inner_array[2].type as type3
from yourtable t
--explode upper array
lateral view outer explode(col_name) a1 as inner_array