Reputation: 147
I am trying to do an operation on Hive/Impala and I don't know how to continue. First, I explain what I want to do. I have the following table:
Well, I want to create a new row for each missing position, and assign it a zero value. The table would look like this:
I do not know if it is possible to create this functionality in Hive or Impala, either one would suit me.
Thanks a lot!
Upvotes: 0
Views: 475
Reputation: 147
Based on @GordonLinoff's answer, i get what I want, but i made some changes. Basically, it is what he says, but splitting his answer in two different queries. This is because in Hive you can not do LATERAL VIEW and JOIN in the same query. The solution would be:
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
Thanks Gordon!
Upvotes: 0
Reputation: 1269563
You can use a trick in Hive where you generate a string of spaces and then split the string into an array and turn the array into a 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;
Upvotes: 1