pig tutorial - apache pig tutorial - Apache Pig - AddDuration() - pig latin - apache pig - pig hadoop

What is AddDuration() Function in Apache Pig ?

  • The AddDuration() function accepts a date-time object and a duration objects, and adds the given duration to the date-time object and returns a new date-time object with added duration.
 date time functions in apache pig

Learn apache pig - apache pig tutorial - date time functions in apache pig - apache pig examples - apache pig programs


grunt> AddDuration(datetime, duration)


The Duration is represented in ISO 8601 standard. According to ISO 8601 standard P is placed at the beginning, while representing the duration and it is called as duration designator. Likewise,

  • Y is the year designator. We use this after declaring the year.

Example − P1Y represents 1 year.

  • M is the month designator. We use this after declaring the month.

Example − P1M represents 1 month.

  • W is the week designator. We use this after declaring the week.

Example − P1W represents 1 week

  • D is the day designator. We use this after declaring the day.

Example − P1D represents 1 day.

  • T is the time designator. We use this before declaring the time.

Example − PT5H represents 5 hours.

  • H is the hour designator. We use this after declaring the hour.

Example − PT1H represents 1 hour.

  • M is the minute designator. We use this after declaring the minute.

Example − PT1M represents 1 minute.

  • S is the second designator. We use this after declaring the second.

Example − PT1S represents 1 second.


  • Ensure that we have a file named wikitechy_date.txt in the HDFS directory /pig_data/. This file contains the date-of-birth details of a particular person, id, date and time and some duration according to ISO 8601 standard.


001,1989/09/26 09:00:00,PT1M
002,1980/06/20 10:22:00,P1Y
003,1990/12/19 03:11:44,P3M 
  • We have loaded this file into Pig with a relation named date_duration as given below.
grunt> date_duration = LOAD 'hdfs://localhost:9000/pig_data/wikitechy_date.txt' USING PigStorage(',')
   as (id:int, date:chararray, duration:chararray)

You can add certain Duration to the given date-time object using this method as given below.

grunt> Add_duration_data = foreach date_duration generate(date,duration), 
   AddDuration(ToDate(date,'yyyy/MM/dd HH:mm:ss'), duration);


  • Verify the content of this relation using the Dump operator as given below.
grunt> Dump add_duration_data;


  • The result of the statement will be stored in the relation named add_duration_data.
((1989/09/26 09:00:00,PT1M),1989-09-26 T09:01:00.000+05:30)
((1980/06/20 10:22:00,P1Y),1981-06-20 T10:22:00.000+05:30)
((1990/12/19 03:11:44,P3M),1991-03-19 T03:11:44.000+05:30)

Related Searches to Apache Pig - AddDuration()

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.