在 PIG 中连接带有时间戳的文件

Concatinating files with Timestamp in PIG

如何将时间戳与 pig 生成的输出连接起来。我需要将 pig 生成的输出保存到另一个带有时间戳的文件夹中,以便将来可以将其用作历史数据。我尝试使用 CurrentTime() 但它给了我这样的错误:

2015-03-31 19:29:58,249 [main] ERROR org.apache.pig.tools.grunt.Grunt  - ERROR 1200: <file script.pig, line 1> Cannot expand macro 'CurrentTime'. Reason: Macro must be defined before expansion.

如何定义这个宏?

这是代码:

A = load '/user/root/b2.out';
X = FILTER A BY ( == 'Error') OR (=='Info') OR (=='Warning') OR (=='Critical');
D = FOREACH X GENERATE [=11=],,,,;
store D into CONCAT('/user/root/ELABD/finalout',CurrentTime());

CONCAT 只能在关系内部使用(又名 foreach 语句),因此您不能使用它来构造输出文件位置。

我认为这里有两种可能的解决方案:

在您的 pig 脚本中使用 %declare 语句,该语句使用 bash 中的 date 之类的内容来获取当前时间并将其用作参数,例如

%declare DATETIME `date +%Y-%m-%dT%H-%M-%S`
...
store D into '/user/root/ELABD/finalout/$DATETIME';

或者使用 Oozie 之类的东西来安排你的猪工作,让 Oozie 根据 date/time.

生成你的输出位置