需要在Hive中爆炸数组结构的语法

时间:2017-03-07 18:36:31

标签: struct hive

示例:存在具有列A1和A2的表T1。 A1具有整数类型数据,A2具有数组结构。现在A2有一列C1。查询的语法应该是什么,以找到对应C1的某个值的A1的值?

1 个答案:

答案 0 :(得分:0)

select  distinct
        A1 

from    T1 lateral view explode(A2) a2 as s

where   a2.s.C1 = {myvalue}

<强>演示

with    T1 as
        (
                       select 1 as A1,array(named_struct('c1',7,'c2',1),named_struct('c1',7,'c2',8),named_struct('c1',5,'c2',2)) as A2 
            union all  select 2 as A1,array(named_struct('c1',6,'c2',7),named_struct('c1',9,'c2',9))                             as A2
            union all  select 3 as A1,array(named_struct('c1',7,'c2',7))                                                         as A2
            union all  select 4 as A1,array(named_struct('c1',8,'c2',3),named_struct('c1',3,'c2',4))                             as A2
        )   

select  distinct
        A1

from    T1 lateral view explode(A2) a2 as s

where   a2.s.C1 = 7
;
+----+
| a1 |
+----+
|  1 |
|  3 |
+----+