【问题标题】:Replace Null with Previous Known Row Value in the Same Column in Pig/Hive用 Pig/Hive 中同一列中的先前已知行值替换 Null
【发布时间】:2016-03-04 10:02:13
【问题描述】:

我有一列中有一个空值的数据集:

price  time       id
1      12:00:00   id1
10     12:00:00   id2
NULL   12:05:00   id1
NULL   12:05:00   id2
NULL   12:10:00   id2
2      12:10:00   id1
3      12:15:00   id1
NULL   12:20:00   id1
NULL   12:25:00   id1
4      12:30:00   id1

我想为 Pig 或 Hive 中的每个 id/时间添加先前已知行值的 null 行。

所以,输出应该是:

price  time       id
1      12:00:00   id1
10     12:00:00   id2
**1**   12:05:00   id1
**10**   12:05:00   id2
**10**   12:10:00   id2
2      12:10:00   id1
3      12:15:00   id1
**3**   12:20:00   id1
**3**   12:25:00   id1
4      12:30:00   id1

非常感谢。

编辑:这是我在蜂巢中运行的:

Select price,time, id,last_value(price,true) over (partition by id order by time) as LatestPrice from table;

对于某些行(1000 行)运行良好,但对于更大的集合(24 M 行),在完成 100% 映射器和 100% 缩减器后,该作业仍在过去 1 天运行。有什么建议吗?

【问题讨论】:

  • 你能展示一些你迄今为止尝试过的代码吗?
  • 我看起来像:从表中选择价格、时间、id、last_value(price) over (partition by id order by time) as LatestPrice 从表中;

标签: hive apache-pig


【解决方案1】:

你可以试试这样的。

 select 
    notNullTmp.price, tmp.id, tmp.time
(
    select LAG(b.time, 1) over (PARTITION BY a.id ORDER BY a.time) as prev_time, b.time as time, b.id as id
    FROM 
    (
        select price, time, id
            from table
            where price is NOT NULL
    ) a
    JOIN 
    (
        select price, time, id
            from table
            where price is NULL
    )b 
    ON (a.id = b.id)
) tmp
JOIN 
(
    select price, time, id
        from table
        where price is NOT NULL
) notNullTmp
ON (tmp.id = notNullTmp.id AND tmp.prev_time == notNullTmp.time)


UNION

select price, time, id
    from table
    where price is NOT NULL;

想法是将空价格记录与非空价格记录分开,然后在非空价格记录中查找每个空价格记录id作为条目。为每个空价格条目选择价格后,我们加入此数据与非空价格数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    相关资源
    最近更新 更多