Formatting Date in Generate Statement

后端 未结 2 2012
遇见更好的自我
遇见更好的自我 2020-12-29 00:09

In Pig, I have a statement which basically appends the date to my generated values.

Data = FOREACH Input GENERATE (CurrentTime()),FLATTEN(group), COUNT(guid)         


        
相关标签:
2条回答
  • 2020-12-29 00:54

    If you are using Pig 0.12 or later, you can use ToString(CurrentTime(),'yyyy-MM-dd')

    You can use any datetime type instead of CurrentTime()

    Refer to http://pig.apache.org/docs/r0.12.0/func.html#to-string for date time formats.

    0 讨论(0)
  • 2020-12-29 01:13

    You have several options:

    Convert it with Pig functions :
    E.g:

    A = load ...
    B = foreach A {
      currTime = CurrentTime();
      year = (chararray)GetYear(currTime);
      month = (chararray)GetMonth(currTime);
      day = (chararray)GetDay(currTime);
      generate CONCAT(CONCAT(CONCAT(year, '-'), CONCAT(month, '-')),day) as myDate;
    }
    

    OR pass the date to the script as a parameter:

    pig -f script.pig -param CURR_DATE=`date +%Y-%m-%d`
    

    OR declare it in script:

    %declare CURR_DATE `date +%Y-%m-%d`;
    

    Then refer to the variable as '$CURR_DATE' in the script.

    You may also create a modified CurrentTime UDF in which you convert the DateTime object to the appropriate format with the Joda-Time library.

    The easiest would be to declare the date in the beginning of the script.

    0 讨论(0)
提交回复
热议问题