Apache Pig - SubtractDuration()


Advertisements

This function accepts a date-time object and a duration objects, and subtract the given duration to the date-time object and returns a new date-time object.

Syntax

Here is the syntax of the SubtractDuration() function.

grunt> SubtractDuration(datetime, duration)

Example

Assume that there is a file named date.txt in the HDFS directory /pig_data/. This file contains the date-of-birth details of a particular person, it has person id, date and time and some duration according to ISO 8601 standard.

date.txt

001,1989/09/26 09:00:00,PT1M
002,1980/06/20 10:22:00,P1Y
003,1990/12/19 03:11:44,P3M 

And, we have loaded this file into Pig with a relation named date_duration as shown below.

grunt> date_duration = LOAD 'hdfs://localhost:9000/pig_data/date.txt' USING PigStorage(',')
   as (id:int, date:chararray, duration:chararray)

Following is an example of the SubtractDuration() function. You can subtract certain duration from the given date-time object using this method as shown below.

subtractduration_data = foreach date_duration generate(date,duration), 
   SubtractDuration(ToDate(date,'yyyy/MM/dd HH:mm:ss'), duration);

The result of the statement will be stored in the relation named subtractduration_data. Verify the content of this relation using the Dump operator as shown below.

grunt> Dump subtractduration_data;
  
((1989/09/26 09:00:00,PT1M),1989-09-26 T08:59:00.000+05:30)
((1980/06/20 10:22:00,P1Y),1979-06-20 T10:22:00.000+05:30)
((1990/12/19 03:11:44,P3M),1990-09-19 T03:11:44.000+05:30)
apache_pig_date_time_functions.htm
Advertisements