使用hadoop map reduce获取最大薪水员工姓名

时间:2013-08-16 07:45:04

标签: hadoop mapreduce hadoop-partitioning

我是M / R程序的新手。我在HDFS中有一个文件,其中包含此结构中的数据

EMPID,EmpName,DEPT,工资,

1231,USERNAME1,Dept1,5000
1232 USERNAME2,Dept2,6000
1233,USERNAME3,Dept3,7000


.........................

现在我想找到赚取最高工资的员工的姓名

我写了一张地图缩小以找到最高的薪水。在我的mapper类中,我已经发出了这样的输出

output.collect(“最大值”,雇员的工资);

在reducer中我找到了键“max value”的最大值。现在我想在mapper中使用这个值,并找到获得最高工资的员工的名字..我可以发送减速器输出作为输入的映射器?这是完成我的任务的好方法吗?还有其他建议吗?

2 个答案:

答案 0 :(得分:1)

我会让地图发出最高薪水的完整元组。为此,创建一个实现Writable接口(http://hadoop.apache.org/docs/r1.2.0/api/org/apache/hadoop/io/Writable.html)的类(用于值)。也许TupleWritable适合您的需求(并不复杂)。

由于每个地图将有1个值,因此网络不是问题,并且在reducer中接收所有元组数据似乎很好。您的减速器只需要从“最大”值过滤顶部。

对于更复杂的问题,您必须考虑将作业链接起来(http://developer.yahoo.com/hadoop/tutorial/module4.html#chaining

答案 1 :(得分:1)

我可以建议以下解决方案

1. Find the max salary using your mapreduce job

2. Read the max salary from hdfs (it should be in the file in output folder of your job)

3. Save the max salary two configuration, say `configuration.set("max.salary", maxSalary);`

4. Create new mapper-only job. The mapper of this job should read maxSalary value from the configuration in the setup method and filter out employers with salary equal to the maxSalary in map method. Pass your data to this job.

结果,你将

P.S。但是作为更好的方法,我建议你使用HIVEPIG来完成这类任务,因为如果它们不涉及复杂的数学/ buseness逻辑,那么实现它们就会容易得多。高级工具,如蜂巢和猪(以及其他一些)。

相关问题