Hive 将单个 Integer 列分成多行

Hive Divide single Integer column into multiple rows

我在 Hive 中有 table 3 列,我想根据特定值划分最后一列(在我的例子中是 200) 蜂巢 table 结构:-

ID ,Name ,Value 
1,"Jan",800
2,"Mar",200
3,"DEC",350
4,"APR",400

我希望输出为

ID ,Name ,Value 
1,"Jan",200
1,"Jan",200
1,"Jan",200
1,"Jan",200
2,"Mar",200
3,"DEC",200
3,"DEC",150
4,"APR",200
4,"APR",200

我尝试使用侧视图爆炸但没有给我正确的结果

ceil(value/200) 给出给定值要生成的行数。您可以使用 split(space(...)) + lateral view posexplode 生成行,然后使用 posexplode 返回的位置来计算当前值。

演示:

with mytable as (--demo dataset, use your table instaed of this CTE
select 1 ID,"Jan" Name,800 Value union all
select 2,"Mar",200 union all
select 3,"DEC",350 union all
select 4,"APR",400
)

select id, name, 
       --value 200 is hardcoded in many places, you can pass it as a parameter to the script
       case when (pos+1)*200 > value --remains less than 200
               then value-pos*200 
            else 200 
       end as value
from
(
select id, name, value, int(ceil(value / 200)) num_rows 
  from mytable
)s  
 --generate string of space, split it and posexplode  
 lateral view outer posexplode(split(space(num_rows-1),'')) e as pos, x

结果:

id  name    value
1   Jan      200
1   Jan      200
1   Jan      200
1   Jan      200
2   Mar      200
3   DEC      200
3   DEC      150
4   APR      200
4   APR      200