我有一个数据集存储在 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);
在此之后,我将 temp_input 中的所有数据GROUPED 为:
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);
}
关于hadoop - Apache Pig 从具有组的数据集中获取最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44670922/