【问题标题】:Equivalent of Hive Lateral view outer Explode in Athena (Presto) CROSS JOIN UNNEST等效于 Hive 横向视图外部爆炸在 Athena (Presto) CROSS JOIN UNNEST
【发布时间】:2019-12-31 23:51:24
【问题描述】:

我们正在尝试在 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 在 Athena 中创建视图时,输出如下:

可乐可乐

1   a

1   b

1   c

如果 JSON 数据没有我们在其上创建 UNNEST 的字段的值,则该行将从输出中删除,而 hive 也会为该行提供相应缺失值的 NULL 值。

/hive 中使用的 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);

【问题讨论】:

    标签: amazon-web-services hive presto amazon-athena


    【解决方案1】:

    选择 * 从 (test_lateral CROSS JOIN UNNEST(coalesce("root"."colb",array[null])) t (alias))

    作品

    【讨论】:

    • 这很好用,我想补充一点,我的 presto 版本 (317) 中的合并不适用于空数组,我必须使用 IF(cardinality("root"."colb") = 0 ,array[null], "root"."colb")
    【解决方案2】:

    显然,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))
    

    【讨论】:

      猜你喜欢
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 2017-04-04
      • 2018-01-02
      • 2022-08-23
      • 2023-03-06
      相关资源
      最近更新 更多