Apache PIG:获取星期几并相应地拆分 [英] Apache PIG: Get the day of the week and split accordingly
问题描述
我需要在两个日期之间拆分日期,并从中忽略星期六和星期日.0.11.1 上的内置函数将有助于获取星期几,但如何确定是星期六还是星期日?任何人都知道吗?我的预期输出如下所述.
I need to split dates between two and ignore saturday and sunday from it. Built in function on 0.11.1 will help to get day of the week but how to find out whether that is saturday or Sunday? Anyone has any idea of it? My expected output described below.
输入:
用户从日期至今
拉吉 10/3/2013 10/8/2013
Raj 10/3/2013 10/8/2013
詹姆斯 10/4/2013 10/7/2013
James 10/4/2013 10/7/2013
等等.
预期输出:
拉吉 10/3/2013
Raj 10/3/2013
拉吉 10/4/2013
Raj 10/4/2013
拉吉 10/7/2013
Raj 10/7/2013
拉吉 10/8/2013
Raj 10/8/2013
詹姆斯 10/4/2013
James 10/4/2013
詹姆斯 10/7/2013
James 10/7/2013
推荐答案
由于 Pig DateTime 对象实际上是以毫秒为单位的 Unix 纪元时间,这可以通过开箱即用的 Pig 运算符轻松完成.
Since the Pig DateTime objects are really Unix epoch time in milliseconds, this can be easily done with out-of-the-box Pig operators.
(DaysBetween(ToDate('10/3/2013','MM/dd/yyyy'),ToDate(0L)) + 4L) % 7
- 在 0...6 范围内产生一个 long,其中 0 = Sun,1 = Mon,...等
- 0L 代表 1/1/1970,一个星期四
- 添加 4L 天使得 0 = 星期天
让自己满意,这是来自 Unix 命令行的:
Satisfy yourself that this is so from Unix command line:
$> date -d '1/1/1970' +%w-%a
4-Thu
$> date -d '10/3/2013' +%w-%a
4-Thu
当然,如果您对 UDF 感到满意,而且这是一个常见的要求,那么这是最好的解决方案.
Of course, if you are comfortable with UDF, and this is a commonly occurring requirement, that's the best solution.
卡特海岸
这篇关于Apache PIG:获取星期几并相应地拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!