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.
输入:
用户Fromdate Todate
User Fromdate Todate
Raj 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
等.
预期输出:
Raj 10/3/2013
Raj 10/3/2013
拉杰10/4/2013
Raj 10/4/2013
拉杰2013年10月7日
Raj 10/7/2013
拉杰2013年10月8日
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,其中0 =太阳,1 =星期一,...等等
- 0L代表1970年1月1日,星期四
- 增加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.
Carter Shore
Carter Shore
这篇关于Apache PIG:获取星期几并进行相应拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!