Pig Latin 跨组求和

Summing across groups in Pig Latin

我有这样的数据行:

user    startTimestamp endTimestamp   durationForeground    application
11  1409239321000  1409239395000  1     IEXPLORE.EXE
11  1409239322000  0    19      IEXPLORE.EXE
11  1409239341000  0    18      IEXPLORE.EXE
11  1409239359000  0    0       IEXPLORE.EXE
11  1409239359000  0    7       IEXPLORE.EXE
11  1409239366000  0    6       IEXPLORE.EXE
11  1409239372000  0    10      IEXPLORE.EXE
11  1409239382000  0    13      IEXPLORE.EXE
11  1409239395000  1409239446000  9     MSPAINT.EXE
11  1409239404000  0    4       MSPAINT.EXE
11  1409239408000  0    13      MSPAINT.EXE
11  1409239421000  0    12      MSPAINT.EXE
11  1409239433000  0    5       MSPAINT.EXE
11  1409239438000  0    8       MSPAINT.EXE

我希望能够对每个小组的所有 durationForegrounds 求和;其中一个组从具有 endTimestamp 的行开始,并在下一次开始之前的行结束。

(原因是 endTimestamp 和 startTimestamp 之间的差异为我们提供了应用程序的 运行 时间,而 durationForeground 的总和为我们提供了应用程序在前台的时间。)

Pig 可以做到吗?

一个选项可能是您需要按 userapplication 对数据进行分组,然后得到 durationForeground 的总和。

示例

输入

11      1409239321000   1409239395000   1       IEXPLORE.EXE
11      1409239322000   0       19      IEXPLORE.EXE
11      1409239341000   0       18      IEXPLORE.EXE
11      1409239359000   0       0       IEXPLORE.EXE
11      1409239359000   0       7       IEXPLORE.EXE
11      1409239366000   0       6       IEXPLORE.EXE
11      1409239372000   0       10      IEXPLORE.EXE
11      1409239382000   0       13      IEXPLORE.EXE
11      1409239395000   1409239446000   9       MSPAINT.EXE
11      1409239404000   0       4       MSPAINT.EXE
11      1409239408000   0       13      MSPAINT.EXE
11      1409239421000   0       12      MSPAINT.EXE
11      1409239433000   0       5       MSPAINT.EXE
11      1409239438000   0       8       MSPAINT.EXE

PigScript:

A = LOAD 'input' USING PigStorage() AS(user:int,startTimestamp:long,endTimestamp:long,durationForeground:long,application:chararray);
B = GROUP A BY (user,application);
C = FOREACH  B GENERATE FLATTEN(group),SUM(A.durationForeground);
DUMP C;

输出:

(11,MSPAINT.EXE,51)
(11,IEXPLORE.EXE,74)

在上述方法中,我假设所有输入字段都由制表符 (\t) 分隔。