Apache Pig - GetMilliSecond()


This function accepts a date-time object as a parameter and returns the milliseconds of the current second of a given date-time object.


Here is the syntax of the GetMilliSecond() function.

grunt> GetMilliSecond(datetime)


Assume that there is a file named date.txt in the HDFS directory /pig_data/ as shown below. This file contains the date-of-birth details of a particular person, it has person id, date and time.


001,1989/09/26 09:00:00
002,1980/06/20 10:22:00
003,1990/12/19 03:11:44 

And, we have loaded this file into Pig with a relation named date_data as shown below.

grunt> date_data = LOAD 'hdfs://localhost:9000/pig_data/date.txt' USING PigStorage(',')
   as (id:int,date:chararray);

Following is an example of the GetMilliSecond() function. The GetMilliSecond() function will retrive the milliseconds of the current second from the given date-time object. Therefore, First of all let’s generate the date-time objects of all employees using todate() function as shown below.

grunt> todate_data = foreach date_data generate ToDate(date,'yyyy/MM/dd HH:mm:ss')
   as (date_time:DateTime );
grunt> Dump todate_data;

Now, let’s get the seconds from the birth time of each employee using GetMilliSecond() function and store it in the relation named getmillisecond_data as shown below.

grunt> getmillisecond_data = foreach todate_data generate (date_time), GetMilliSecond(date_time);

Now verify the contents of the getmillisecond_data relation using Dump operator as shown below.

grunt> Dump getmillisecond_data;