pig tutorial - apache pig tutorial - Apache Pig - MonthsBetween(datetime1, datetime2) Function - pig latin - apache pig - pig hadoop
What is MonthsBetween(datetime1,datetime2) function in Apache Pig ?
- The MonthsBetween(datetime1,datetime2) function will accept the two date-time objects and also calculates the number of months which is done between the two given date-time objects.
- The MonthsBetween(datetime1,datetime2) function will returns the number of months which is done between date-time datetime1 and datetime2.
- The MonthsBetween(datetime1,datetime2) calculates the fractional portion of the result and also considers the difference of the time components which is given between datetime1 and datetime2.
Syntax
grunt>MonthsBetween(datetime1, datetime2)
Example
- Ensure that there is 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.
wikitechy_doj_dob.txt
131,27/10/1990 09:00:00,17/02/2016 09:00:00
132,21/07/1981 10:22:00,11/09/2012 09:00:00
133,20/01/1992 03:11:44,26/11/2013 09:00:00
- We have loaded the file into Pig with a relation name 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 can calculate the number of minutes which is done between date-of-birth and date-of-joining of the employees by using the MonthsBetween() function which is given below:
grunt>monthsbetween_data = foreachdoj_dob_data generate
MinutesBetween(ToDate(doj,'dd/MM/yyyy HH:mm:ss'),ToDate(dob,'dd/MM/yyyy HH:mm:ss'));
Verification
- We need to verify the contents of the relation by using the Dump operator which is given below:
grunt> Dump monthsbetween;
Output
- The above statement stores the result in the relation named monthsbetween_data
(300)
(374)
(262)