Hadoop PIG Max of Tuple

时间:2012-12-27 14:06:26

标签: hadoop apache-pig

如何在Pig中找到元组的MAX

我的代码如下所示:

A,20
B,10
C,40
D,5

data = LOAD 'myData.txt' USING PigStorage(',') AS key, value;
all = GROUP data ALL;
maxKey = FOREACH all GENERATE MAX(data.value);
DUMP maxKey;

这会返回40,但我想要完整的键值对:C,40。有什么想法吗?

2 个答案:

答案 0 :(得分:7)

这适用于Pig 0.10.0:

data = LOAD 'myData.txt' USING PigStorage(',') AS (key, value: long);
A = GROUP data ALL;
B = FOREACH A GENERATE MAX(data.value) AS val;
C = FILTER data BY value == (long)C.val;
DUMP C;

答案 1 :(得分:3)

试试这个:

data = LOAD 'myData.txt' USING PigStorage(',') AS (key: chararray, value: int);

sorted = ORDER data BY value DESC;

limited = LIMIT sorted 1;

projected = FOREACH limited GENERATE key;

DUMP projected;