- Apache Pig Tutorial
- Apache Pig - Home
- Apache Pig Introduction
- Apache Pig - Overview
- Apache Pig - Architecture
- Apache Pig Environment
- Apache Pig - Installation
- Apache Pig - Execution
- Apache Pig - Grunt Shell
- Pig Latin
- Pig Latin - Basics
- Load & Store Operators
- Apache Pig - Reading Data
- Apache Pig - Storing Data
- Diagnostic Operators
- Apache Pig - Diagnostic Operator
- Apache Pig - Describe Operator
- Apache Pig - Explain Operator
- Apache Pig - Illustrate Operator
- Grouping & Joining
- Apache Pig - Group Operator
- Apache Pig - Cogroup Operator
- Apache Pig - Join Operator
- Apache Pig - Cross Operator
- Combining & Splitting
- Apache Pig - Union Operator
- Apache Pig - Split Operator
- Pig Latin Built-In Functions
- Apache Pig - Eval Functions
- Load & Store Functions
- Apache Pig - Bag & Tuple Functions
- Apache Pig - String Functions
- Apache Pig - date-time Functions
- Apache Pig - Math Functions
- Other Modes Of Execution
- Apache Pig - User-Defined Functions
- Apache Pig - Running Scripts
- Apache Pig Useful Resources
- Apache Pig - Quick Guide
- Apache Pig - Useful Resources
- Apache Pig - Discussion
Apache Pig - HoursBetween()
This function accepts two date-time objects and calculates the number of hours between the two given date-time objects.
Syntax
Here is the syntax of the HoursBetween() function.
grunt> HoursBetween(datetime1, datetime2)
Example
Assume that there is a file named doj_dob.txt in the HDFS directory /pig_data/. This file contains the date-of-birth and date-of-joining details of a particular person, id, dateof-birth, and date-of-joining.
doj_dob.txt
001,26/09/1989 09:00:00,16/01/2015 09:00:00 002,20/06/1980 10:22:00,10/08/2011 09:00:00 003,19/12/1990 03:11:44,25/10/2012 09:00:00
And, we have loaded this file into Pig with a relation named doj_dob_data as shown below.
doj_dob_data = LOAD 'hdfs://localhost:9000/pig_data/doj_dob.txt' USING PigStorage(',') as (id:int, dob:chararray, doj:chararray);
Let us now calculate the number of hours between date-of-birth and date-of-joining of the employees using the HoursBetween() function as shown below.
hoursbetween_data = foreach doj_dob_data generate HoursBetween(ToDate(doj, 'dd/MM/yyyy HH:mm:ss'),ToDate(dob,'dd/MM/yyyy HH:mm:ss'));
The above statement stores the result in the relation named hoursbetween_data. Verify the contents of the relation using the Dump operator as shown below.
grunt> Dump HoursBetween; (221832) (272950) (191549)
To Continue Learning Please Login
Login with Google