apache pig从具有组的数据集获取max

bxgwgixi  于 2021-06-01  发布在  Hadoop
关注(0)|答案(1)|浏览(314)

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

  1. US,Arizona,51.7
  2. US,California,56.7
  3. US,Bullhead City,51.1
  4. India,Jaisalmer,42.4
  5. Libya,Aziziya,57.8
  6. Iran,Lut Desert,70.7
  7. India,Banda,42.4

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

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

在此之后,我将temp\u input中的所有数据分组为:

  1. group_country = GROUP temp_input BY country;

当我在组\国家转储数据时,屏幕上显示以下输出:

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

数据集分组后,我试图通过以下查询获取每个国家组的国家名称和个人最高温度:

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

这将显示一个错误,如下所示:

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

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

w6mmgewl

w6mmgewl1#

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

  1. desribe group_country;

应该给你一个输出,比如:

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

然后查询:

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

输出:

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

根据评论更新:

  1. finaldata = foreach group_country {
  2. orderedset = order temp_input by temp DESC;
  3. maxtemps = limit orderedset 1;
  4. generate flatten(maxtemps);
  5. }
展开查看全部

相关问题