【问题标题】:Data Summarization in Apache Pig/Apache Hive For Given Date RangeApache Pig/Apache Hive 中给定日期范围的数据汇总
【发布时间】:2016-10-29 05:25:30
【问题描述】:

我有一个要求,我需要对作为输入提供的日期范围进行数据汇总。更具体地说:如果我的数据如下所示:

Input:
Id|amount|date
1 |10    |2016-01-01
2 |20    |2016-01-02
3 |20    |2016-01-03
4 |20    |2016-09-25
5 |20    |2016-09-26
6 |20    |2016-09-28

如果我想要 9 月份的总结,那么 我需要计算 4 个范围内的记录数:

  1. 当前日期,即 9 月的每一天。
  2. 周开始日期(根据当前日期一周的星期日)到当前日期,例如。如果当前日期为 2016-09-28,则周开始日期为 2016-09-25 2016 年 9 月 25 日至 2016 年 9 月 28 日期间的记录数。
  3. 开始日期到当前日期,即从 2016-09-01 到当前日期。
  4. 年开始日期到当前日期,即从2016-01-01到当前日期的记录数。

所以我的输出应该有一个记录,每个月的每一天都有 4 列(在这种情况下,月份是九月),类似于

Output:

 Current_Date|Current_date_count|Week_To_Date_Count|Month_to_date_Count|Year_to_date_count

 2016-09-25  |1                 |1                 |1                  |4
 2016-09-26  |1                 |2                 |3                  |5
 2016-09-28  |1                 |3                 |3                  |6

重要提示:我只能传递 2 个变量,即范围开始日期和范围结束日期。剩余计算需要是动态的。

提前致谢

【问题讨论】:

    标签: hive apache-pig hiveql apache-pig-grunt


    【解决方案1】:

    您可以按年加入,然后分别测试每个条件(使用sum(if())):

    select  a.date, sum(if(a.date=b.date,1,0)), 
                    sum(if(month(a.date)=month(b.date) and weekofyear(a.date)=weekofyear(b.date),1,0)),
                    sum(if(month(a.date)=month(b.date),1,0)),
                    count(*) from
    (select * from input_table where date >= ${hiveconf:start} and date <${hiveconf:end}) a, 
    (select * from input_table where date <${hiveconf:end}) b 
    where year(a.date)=year(b.date) and b.date <= a.date group by a.date;
    

    【讨论】:

    • 谢谢亚历克斯!!。但这里的几点是:
    • 谢谢亚历克斯!!。但这里的几点是: 1.weekofyear 函数将星期一视为周开始日期。在我的情况下,星期日需要是星期的开始日期,并且 2. 笛卡尔积在我的基表中使用最少的数据永远运行,即使在使用 Tez 之后也是如此。
    • @AKN_1513 然后您需要实现自己的自定义 weekofyear UDF。关于速度,请尝试加入年份的查询..仅尝试顶部选择的count(*),尝试找出导致延迟的原因。
    • @AKN_1513 如果您没有意识到,PIG 确实不是处理日历日期的好工具。所以是的,当你尝试做“这类事情”时,你需要经常使用自定义函数或解决方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    相关资源
    最近更新 更多