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



What is GetWeek() in Apache Pig ?

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

Syntax:

grunt> GetWeek(datetime)

Example:

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

Wiki_date.txt

001,1989/09/26 09:00:00
002,1980/06/20 10:22:00
003,1990/12/19 03:11:44
  • 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/wiki_date.txt' USING PigStorage(',')
   as (id:int,date:chararray);
  • An example of the GetWeek() function.
  • It will retrive the current week from the given date-time object.
  • Hence, we 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;
(1989-09-26T09:00:00.000+05:30)
(1980-06-20T10:22:00.000+05:30)
(1990-12-19T03:11:44.000+05:30) 
  • Let us now get the month from the date-of-birth of each employee using GetWeek() and store it in the relation named getweek_data.
grunt> getweek_data = foreach todate_data generate (date_time), GetWeek(date_time);

Now, verify the contents of the getweek_data relation using the Dump operator.

grunt> Dump getWeek_data;

Output:

(1989-09-26T09:00:00.000+05:30,39) 
(1980-06-20T10:22:00.000+05:30,25) 
(1990-12-19T03:11:44.000+05:30,51)

Related Searches to Apache Pig - GetWeek()