使用结构检索数组数组中的值

问题描述

我在 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...