猪:获取组中第一次出现的变量(同时聚合其他变量)?

Pig: Get first occurrence of variable in a group (while aggregating other variables)?

我有一个看起来像

的数据集
gr    col1  col2
A     2    'haha'
A     4    'haha'
A     3    'haha'
B     5    'hoho'
B     1    'hoho'

如您所见,在每个组 gr 中有一个数字变量 col1 和一些字符串变量 col2 ,它们在每个组中都是相同的。

如何在PIG中得到如下伪代码?

foreach group gt : generate the mean of col1 and get the first occurrence of col2

所以输出看起来像

gr    mean  name
A     3    'haha'
B     3    'hoho'

谢谢!

GROUP BY gr,col2 并得到 col1 的 AVG。假设字段以制表符分隔。

PigScript

A = load 'test6.txt' USING PigStorage('\t') as (gr:chararray,col1:int,col2:chararray);
B = GROUP A BY (gr,col2);
C = FOREACH B  GENERATE FLATTEN(group) as (gr,name),AVG(A.col1) as mean;
DUMP C;

注意:如果您希望按顺序排列它们,请添加额外的步骤

D = FOREACH C  GENERATE [=11=] as gr, as mean, as name;

输出