如何在当前时间添加天数?

时间:2015-06-25 11:48:58

标签: apache-pig

这是我的要求

A = Load 'number' using PigStorage(',') as (id:chararray,name:chararray,join_date:chararray)
B = FOREACH A GENERATE (join_date is NULL ?AddDuration(CurrentTime(),1000):join_date);
dump b;

如何实现这一目标?

1 个答案:

答案 0 :(得分:3)

As per API : AddDuration(datetime, duration); datetime : A datetime object. duration : The duration string in ISO 8601 format. We have to pass a duration string in ISO 8601 format as the second argument. Ref : http://pig.apache.org/docs/r0.12.0/func.html#add-duration https://en.wikipedia.org/wiki/ISO_8601 For the use case shared in question: Input : Note that the first value in date field is null or empty () (20150625) Pig Script : date_test = LOAD '/Users/muralirao/learning/pig/a.csv' USING PigStorage('\t') AS (join_date:chararray); default_date_plus_2_days = FOREACH date_test GENERATE ((join_date IS NULL) ? AddDuration(CurrentTime(),'P2D') : ToDate(join_date,'yyyyMMdd')) AS join_date; display = FOREACH default_date_plus_2_days GENERATE ToString(join_date,'yyyyMMdd') as display_date; DUMP display; N.B : The second argument to AddDuration here is 'P2D' i.e. 2 Days. Check ISO_8601 standard of representing Duration object. Output : DUMP display (20150627) (20150625)