在Hive中,explode()和侧面视图explode()有什么区别

时间:2018-08-03 04:24:45

标签: hive

假设有一个表employee

+-----------+------------------+
| col_name  |    data_type     |
+-----------+------------------+
| id        | string           |
| perf      | map<string,int>  |
+-----------+------------------+

以及该表中的数据:

+-----+------------------------------------+--+
| id  |                perf                |
+-----+------------------------------------+--+
| 1   | {"job":80,"person":70,"team":60}   |
| 2   | {"job":60,"team":80}               |
| 3   | {"job":90,"person":100,"team":70}  |
+-----+------------------------------------+--+

我尝试了以下两个查询,但是它们都返回相同的结果:

1. select explode(perf) from employee;
2. select key,value from employee lateral view explode(perf) as key,value;

结果:

+---------+--------+--+ | key | value | +---------+--------+--+ | job | 80 | | team | 60 | | person | 70 | | job | 60 | | team | 80 | | job | 90 | | team | 70 | | person | 100 | +---------+--------+--+

那么,它们之间有什么区别?我没有找到合适的例子。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

对于您的特殊情况,两个查询都可以。但是,如果没有侧面视图,就不能使用多个explode()函数。因此,以下查询将失败:

select explode(array(1,2)), explode(array(3, 4))

您需要编写如下内容:

select
    a_exp.a,
    b_exp.b
from (select array(1, 2) as a, array(3, 4) as b) t
lateral view explode(t.a) a_exp as a
lateral view explode(t.b) b_exp as b