pig tutorial - apache pig tutorial - Apache Pig - MilliSecondsBetween(datetime1, datetime2) Function - pig latin - apache pig - pig hadoop

What is MilliSecondsBetween(datetime1,datetime2)Function in Apache Pig ?

  • The MilliSecondsBetween(datetime1,datetime2) function accepts the two date-time objects and also calculates the number of milliseconds which is done between the two given date-time objects.
  • The MillisSecondsBetween(datetime1,datetime2) functionreturn the number of whole millisecondsbetweendatetime 1anddatetime2.
  • TheMilliSecondsBetween(datetime 1,datetime 2) will always returns a positive result and also the parameter values are interchangeable.


grunt> MilliSecondsBetween(datetime1, datetime2)


  • Ensure that we have a file named wikitechy_doj_dob.txt in the HDFS directory /pig_data/.
  • The file contains the date-of-birth details and date-of-joining details of a particular person, id, dateof-birth and date-of-joining.


111,26/09/1989 09:00:00,16/01/2015 09:00:00
112,20/06/1980 10:22:00,10/08/2011 09:00:00
113,19/12/1990 03:11:44,25/10/2012 09:00:00 
  • We have loaded the file into Pig with a relation name called doj_dob_data which is given below:
doj_dob_data = LOAD 'hdfs://localhost:9000/pig_data/wikitechy_doj_dob.txt' USING PigStorage(',')
as (id:int, dob:chararray, doj:chararray);
  • We will calculate the number of milli seconds which is given between date-of-birth and date-of-joining of all the employees by using the MilliSecondsBetween() function which is given below:
grunt>millisecondsbetween_data = foreachdoj_dob_data generate 
MilliSecondsBetween(ToDate(doj,'dd/MM/yyyy HH:mm:ss'), ToDate(dob,'dd/MM/yyyy HH:mm:ss'));


  • We need to verify the contents of the relation by using the Dump operator as shown below.
grunt> Dump millisecondsbetween_data;


  • The above statement stores in the relation named millisecondsbetween_data

Related Searches to Apache Pig GetMilliSecond(datetime) Function