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.

Syntax

grunt> GetHour(datetime)

Example

  • 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.

wikitechy_date.txt

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;  
(1991-08-26T09:00:00.000+05:30)
(1993-05-20T10:22:00.000+05:30) 
(1997-10-19T03:11:44.000+05:30)
  • 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);

Verification

grunt> Dump gethour_data;  
(1990-08-26T09:00:00.000+05:30,9) 
(1993-05-20T10:22:00.000+05:30,10) 
(1997-10-19T03:11:44.000+05:30,3)

Related Searches to Apache Pig GetHour(datetime) Function