Hadoop PIG Max of Tuple
Posted
技术标签:
【中文标题】Hadoop PIG Max of Tuple【英文标题】: 【发布时间】:2012-12-27 14:06:26 【问题描述】:如何在 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
。有什么想法吗?
【问题讨论】:
【参考方案1】:这适用于 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;
【讨论】:
请注意:在计算“C”数据时,应使用 B.val 而不是 C.val 进行过滤 我想第二个@Zibi。最后一个声明应该是C = FILTER data BY value == (long)B.val;
,而不是C = FILTER data BY value == (long)C.val;
。感谢@Frederic Schmaljohann 的解决方案。这在 Pig 0.10 上对我有用。【参考方案2】:
试试这个:
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;
【讨论】:
以上是关于Hadoop PIG Max of Tuple的主要内容,如果未能解决你的问题,请参考以下文章