explode函数是可以将array或者map拆分成多列或者多行的函数,当传入的array是长度为0的空数组时,在查询结果集中是不会包含该数据的(类似数据库中的表A与空表做笛卡尔积,返回为空),如果要包含空数据,需要在lateral view
后加上outer
关键字。
select customer_name,
country,
tbl3.tmp_area
from (
select customer_name,
tbl2.country,
tbl2.area_list
from etl_wc_abroad_appointment
lateral view explode(default.jsonarray2hivearray(way_country)) tbl1 as tmp_country
lateral view json_tuple(tbl1.tmp_country,'country','area') tbl2 as country,area_list
) t
lateral view outer explode(default.jsonarray2hivearray(area_list)) tbl3 as tmp_area
;
default.jsonarray2hivearray()函数是将json数组转换为hive中数组的UDF,可以看出与split产生的空数组结果是一样的,但通过explode之后结果不一致。
/> select split('',',');
+------+--+
| _c0 |
+------+--+
| [] |
+------+--+
1 row selected (20.611 seconds)
/> select default.jsonarray2hivearray('[]');
+------+--+
| _c0 |
+------+--+
| [] |
+------+--+
/> select explode(split('',','));
+------+--+
| col |
+------+--+
| |
+------+--+
1 row selected (22.956 seconds)
/> select explode(default.jsonarray2hivearray('[]'));
+------+--+
| col |
+------+--+
+------+--+