pig tutorial - apache pig tutorial - Apache Pig GetHour(datetime) Function - pig latin - apache pig - pig hadoop

What is GetHour(datetime) Function in Apache Pig ?

  • This function accepts a date-time object as parameter and returns the current hour of the current day of a given date-time object.
  • The GetHour() method will returns the hour of the specified date and time.
  • The function getHour() method will returns the hour in the specified date which is done according to local time.


grunt> GetHour(datetime)


  • Ensure that we have a file named wikitecky_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.


111,1991/08/26 09:00:00
112,1993/05/20 10:22:00
113,1997/10/19 03:11:44
  • We have loaded the file into Pig with a relation name called date_data which is given below:
grunt> date_data = LOAD 'hdfs://localhost:9000/pig_data/wikitechy_date.txt' USING PigStorage(',')
   as (id:int,date:chararray);
  • Following is an example of the GetHour() function.
  • The GetHour() function will retrive the hour of the day from the given Date-Time object.
  • Hence, you can generate the Date-Time objects of all employees using todate() function.
grunt> todate_data = foreach date_data generate ToDate(date,'yyyy/MM/dd HH:mm:ss')
   as (date_time:DateTime );

grunt> Dump todate_data;  
  • We will get the hour from the birth time of the employees which is done using GetDay() function and store it in the relation named called gethour_data.
grunt> gethour_data = foreach todate_data generate (date_time), GetHour(date_time);


grunt> Dump gethour_data;  

Related Searches to Apache Pig GetHour(datetime) Function