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

时间:2017-06-21 08:39:10

标签: hadoop mapreduce hdfs apache-pig

我有一个存储在 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

我应该采取什么措施来解决此错误并获取所需的结果。 感谢所有帮助。

1 个答案:

答案 0 :(得分:4)

在转换关系时,使用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);
}