在 Generate 语句中格式化日期 [英] Formatting Date in Generate Statement
问题描述
在 Pig 中,我有一个语句,它基本上将日期附加到我生成的值中.
In Pig, I have a statement which basically appends the date to my generated values.
Data = FOREACH Input GENERATE (CurrentTime()),FLATTEN(group), COUNT(guid)oas Cnt;
输出为我提供了 ISO8601 中的日期 2013-05-25T09:01:38.914-04:00
.
The output gives me the date 2013-05-25T09:01:38.914-04:00
in ISO8601.
我怎样才能像 "YYYY-MM-DD"
一样做到这一点?
How can I make this just as "YYYY-MM-DD"
?
推荐答案
您有几个选择:
You have several options:
用 Pig 函数转换它:
例如:
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 将日期作为参数传递给脚本:
OR pass the date to the script as a parameter:
pig -f script.pig -param CURR_DATE=`date +%Y-%m-%d`
或在脚本中声明:
%declare CURR_DATE `date +%Y-%m-%d`;
然后在脚本中将变量引用为 '$CURR_DATE'
.
Then refer to the variable as '$CURR_DATE'
in the script.
您还可以创建一个修改的 CurrentTime UDF,您可以在其中使用 Joda-Time 库.
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.
这篇关于在 Generate 语句中格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!