我们正在尝试在Athena中创建一个Unnest视图,该视图等效于其中包含数组字段的JSON数据的Hive横向视图,然后如果unnest为null,则将删除父ky。
以下是我们尝试在其上创建视图的示例JSON。
{"root":{"colA":"1","colB":["a","b","c"]}}
{"root":{"colA":"2"}}
Hive视图中上述数据的输出如下:
+----------------------+----------------------+--+
| test_lateral_v.cola | test_lateral_v.colb |
+----------------------+----------------------+--+
| 1 | a |
| 1 | b
| 1 | c |
| 2 | NULL |
+----------------------+----------------------+--+
但是当我们尝试在雅典娜中使用以下CROSS JOIN UNNEST创建视图时,输出如下:
可乐豆
1 a
1 b
1 c
如果JSON数据不包含我们在其上创建UNNEST的字段的值,则该行将从输出中删除,而hive也会为该行提供相应的缺少值的NULL值。
配置单元中使用的/ DDL /
create external table if not exists test_lateral(
root struct<
colA: string,
colB: array<
string
>
>
)
ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe'
Stored as textfile
location "<hdfs_location>";
create view test_lateral_v
(colA,colB)
as select
root.colA,
alias
from test_lateral
lateral view outer explode (root.colB) t as alias;
/ DDL /
create external table if not exists test_lateral(
root struct<
colA: string,
colB: array<
string
>
>
)
ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe'
Stored as textfile
location "<s3_location>";
create view test_lateral_v
as select
root.colA,
alias as colB
from test_lateral
cross join unnest (root.colB) as t (alias);
答案 0 :(得分:1)
选择 * 从 (test_lateral CROSS JOIN UNNEST(coalesce(“ root”。“ colb”,array [null]))t(别名))
有效
答案 1 :(得分:0)
很显然,当未嵌套的数组为null或为空时,CROSS JOIN
UNNEST
不产生任何行,但是您可以使用LEFT JOIN UNNEST
:
SELECT * test_lateral
LEFT JOIN UNNEST("root"."colb") t(alias) ON true;
自Presto 319起可用。
在此之前,您可以使用coalesce
将空数组替换为虚拟值。 (这假设您的数据中没有空数组)。
SELECT *
FROM test_lateral
CROSS JOIN UNNEST(coalesce("root"."colb", ARRAY[NULL])) t (alias))