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



What is GetSecond() in Apache Pig ?

  • The getSeconds() method returns the seconds (from 0 to 59) of the specified date and time.
  • This function accepts a date-time object as a parameter and returns the seconds of the current minute of a given date-time object.

Syntax:

grunt> GetSecond(datetime)

Example:

  • Ensure that you 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,1979/09/26 09:10:00
02,1980/06/20 10:22:00
03,1999/12/19 03:10:22
  • You have loaded this file into Pig with a relation named date_data as given below.
grunt> date_data = LOAD 'hdfs://localhost:9000/pig_data/wikitechy_date.txt' USING PigStorage(',')
   as (id:int,date:chararray);
  • An example of the GetSecond() function. It retrives the seconds of a minute from the given date-time object.
  • Hence,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;
(1979/09/26 09:10:00:00.000+05:30) 
(1980-06-20T10:22:00.000+05:30) 
(1999/12/19 03:10:22.000+05:30) 
  • We get the seconds from the birth time of each employee using GetSecond() function and store it in the relation named wiki_getsecond_data as given below.
grunt> wiki_getsecond_data = foreach todate_data generate (date_time),GetSecond(date_time);

Verification:

You have verify the contents of the wiki_getsecond_data relation using the Dump operator as given below.

grunt> Dump wiki_getsecond_data;

Output:

(1979/09/26 09:10:00.000+05:30,0)
(1980-06-20T10:22:00.000+05:30,0)
(1990-12-19T03:11:44.000+05:30,44)

Related Searches to Apache Pig - GetSecond()