Apache Pig 从具有组的数据集中获取最大值

Apache Pig fetch max from a data-set that has Groups

我有一个数据集存储在 HDFS 中一个名为 temp.txt 的文件中,如下所示:

US,Arizona,51.7
US,California,56.7
US,Bullhead City,51.1
India,Jaisalmer,42.4
Libya,Aziziya,57.8
Iran,Lut Desert,70.7
India,Banda,42.4

现在,我通过以下命令将其加载到 Pig 内存中:

temp_input = LOAD '/WC/temp.txt' USING PigStorage(',') as 
(country:chararray,city:chararray,temp:double);

在此之后,我 GROUPED temp_input 中的所有数据为:

 group_country = GROUP temp_input BY country;

当我在 group_country 中转储数据时,屏幕上显示以下输出:

(US,{(US,Bullhead City,51.1),(US,California,56.7),(US,Arizona,51.7)})
(Iran,{(Iran,Lut Desert,70.7)})
(India,{(India,Banda,42.4),(India,Jaisalmer,42.4)})
(Libya,{(Libya,Aziziya,57.8)})

对数据集进行分组后,我尝试通过以下查询在 group_country 中获取每个国家/地区的名称和个人最高温度:

max_temp = foreach group_country generate group,max(temp);

这会产生如下所示的错误:

017-06-21 13:20:34,708 [main] ERROR org.apache.pig.tools.grunt.Grunt - ERROR 
1070: Could not resolve max using imports: [, java.lang., 
org.apache.pig.builtin., org.apache.pig.impl.builtin.]
Details at logfile: /opt/ecosystems/pig-0.16.0/pig_1498026994684.log

我的下一步应该是什么来解决这个错误并获取所需的结果。 感谢所有帮助。

在转换关系 pig 时使用 describe relationname 这将有助于了解如何迭代。所以在你的情况下:

desribe group_country;

应该给你这样的输出:

group_country: {group: chararray,temp_input: {(country: chararray,city: chararray,temp: double)}}

然后查询:

max_temp = foreach group_country GENERATE group,MAX(temp_input.temp);

输出:

(US,56.7) (Iran,70.7) (India,42.4) (Libya,57.8)

根据评论更新:

finaldata = foreach group_country {
    orderedset = order temp_input by temp DESC;
    maxtemps = limit orderedset 1;
    generate flatten(maxtemps);
}