我们试图在雅典娜中创建一个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 在 Athena 中创建视图时,下面是输出:
可乐科尔布
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);
选择 * 从 (test_lateral CROSS JOIN UNNEST(coalesce("root"."colb",array[null](( t (alias((
工程
显然,当非嵌套数组为 null 或空时,CROSS JOIN
UNNEST
不会产生任何行,但您可以使用LEFT JOIN UNNEST
:
SELECT * test_lateral
LEFT JOIN UNNEST("root"."colb") t(alias) ON true;
这从 Presto 319 开始可用。 在此之前,您可以使用coalesce
将 null 数组替换为虚拟值。(这假设数据中没有空数组(。
SELECT *
FROM test_lateral
CROSS JOIN UNNEST(coalesce("root"."colb", ARRAY[NULL])) t (alias))