相当于athena(presto)交叉连接unnest中的hive横向视图outer explode

h79rfbju  于 2021-06-24  发布在  Hive
关注(0)|答案(2)|浏览(526)

我们正在尝试在athena中创建一个unnest视图,它相当于json数据的hive横向视图,其中包含数组字段,如果unnest为null,则会删除父ky。
下面是我们试图创建视图的示例json。

{"root":{"colA":"1","colB":["a","b","c"]}}
{"root":{"colA":"2"}}

以上数据在配置单元视图中的输出如下:

+----------------------+----------------------+--+ 

| test_lateral_v.cola  | test_lateral_v.colb  |    
+----------------------+----------------------+--+ 

| 1                    | a                    |    
| 1                    | b                     
| 1                    | c                    |    
| 2                    | NULL                 |    
+----------------------+----------------------+--+

但是当我们试图在雅典娜中创建带有交叉连接的视图时,下面是输出:
可乐可乐

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);
4jb9z9bj

4jb9z9bj1#

显然,
CROSS JOIN UNNEST 当unnested array为null或空时不生成行,但可以使用 LEFT JOIN UNNEST :

SELECT * test_lateral
LEFT JOIN UNNEST("root"."colb") t(alias) ON true;

从普雷斯托319开始提供。在此之前,你可以使用 coalesce 用伪值替换空数组(这假设数据中没有空数组)。

SELECT *
FROM test_lateral
CROSS JOIN UNNEST(coalesce("root"."colb", ARRAY[NULL])) t (alias))
kqlmhetl

kqlmhetl2#

select*from(test_横向交叉连接unnest(coalesce(“root.”“colb”,array[null]))t(alias))
作品

相关问题