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




What is GetMonth() in Apache Pig ?

  • The GetMonth() function accepts a date-time object as a parameter and returns the current month of the current year from the given date-time object.

Syntax:

grunt> GetMonth(datetime)

Example:

  • Ensure that we have a file named wikitechy_date.txt in the HDFS directory /pig_data/ as given below.
  • This file contains the date-of-birth details of a particular person, id, date and time.

Wikitechy_date.txt

01,1869/07/16 09:00:00
02,1980/06/10 10:22:00
03,1996/10/04 03:11:33 
  • You have loaded this file into Pig with a relation named date_data.
grunt> date_data = LOAD 'hdfs://localhost:9000/pig_data/wikitechy_date.txt' USING PigStorage(',')
   as (id:int,date:chararray);
  • An example of the GetMonth() function.
  • It will retrive the current month from the given date-time object.
  • Hence, First of all let’s generate the date-time objects of all employees using todate() function as given below.
grunt> todate_data = foreach date_data generate ToDate(date,'yyyy/MM/dd HH:mm:ss')
   as (date_time:DateTime );
  
grunt> Dump todate_data;
(1869-07-16T09:10:10.000+05:30) 
(1980-06-20T10:22:00.000+05:30) 
(1996-10-04T03:11:33.000+05:30) 
  • You get the month from the date-of-birth of each employee using GetMonth() function and store it in the relation named wiki_getmonth_data.
grunt> wiki_getmonth_data = foreach todate_data generate (date_time), GetMonth(date_time);

Verification:

  • You are verify the contents of the wiki_getmonth_data relation using Dump operator as given below.
grunt> Dump getmonth_data;

Output:

(1869-07-16T09:10:10.000+05:30,7) 
(1980-06-20T10:22:00.000+05:30,6) 
(1996-10-04T03:11:33.000+05:30,10)

Related Searches to Apache Pig - GetMonth()