根据 Hive/Impala 中的值创建新行

Create new rows depending on values in Hive/Impala

我正在尝试对 Hive/Impala 进行手术,但我不知道如何继续。首先,我解释一下我想做什么。我有以下 table:

好吧,我想为每个缺失的位置创建一个新行,并为其分配一个零值。 table 看起来像这样:

我不知道是否可以在 Hive 或 Impala 中创建此功能,两者都适合我。

非常感谢!

你可以在 Hive 中使用一个技巧,生成一串空格,然后将字符串拆分为一个数组,并将该数组转换为 table:

select pe.i, coalesce(t.value, 0) as value
from (select i, x
      from (select max(position) as max_position
            from t
           ) p lateral view 
           posexplode(split(space(p.max_position), ' ')) pe as i, x
     ) pe left join
     t
     on pe.i = t.position;

根据@GordonLinoff 的回答,我得到了我想要的,但我做了一些更改。基本上,这就是他所说的,但将他的答案分成两个不同的问题。这是因为在 Hive 中,您不能在同一个查询中执行 LATERAL VIEW 和 JOIN。解决方案是:

create table t1 as 
select i, x 
from (select max(position) as max_position from t) p 
lateral view posexplode(split(space(p.max_position), ' ')) pe as i, x

select a.i, coalesce(b.value, 0) as value
from t1 a LEFT JOIN t b
on a.i = b.position
where a.i != 0

谢谢戈登!