[Solved-2 Solutions] How to manipulate and compare dates in Pig ?



Problem :

How to manipulate and compare dates in Pig ?

Solution 1:

Use of yearbetween()

  • This function accepts two date-time objects and calculates the number of years between the two given date-time objects.

Syntax

  • The syntax of the YearsBetween() function.
grunt> YearsBetween(datetime1, datetime2) 
  • In Pig 0.11 we can convert the date2 field from chararray to datetime data type using the ToDate() function, and then get the difference between the CurrentTime() and date2 using YearsBetween() and filter according to it.

Example:

g = FILTER f BY YearsBetween(CurrentTime(),ToDate(date2 + ' 01', 'yyyy MM dd'))<3

Solution 2:

  • In pig 11, there are many functions available for comparing dates.

For example:

  • date1:datetime and filter has condition: date1 >= ToDate('1999-01-01')

Related Searches to How to manipulate and compare dates in Pig ?