gpt4 book ai didi

hadoop - 使用 MapReduce 的员工的最大工资

转载 作者:可可西里 更新时间:2023-11-01 16:43:45 25 4
gpt4 key购买 nike

假设我在 HDFS 中有一个包含以下数据的文件:

EmpId,EmpName,Dept,Salary

121,Raj,Dept1,8000
122,Kiran,Dept2,6000
123,John,Dept3,9000

使用 MapReduce 我只想获得最高 Salary 员工的 SalaryEmpName

我能够获得最高 Salary 但无法获得相应的 EmpName。通过在我的 map 类中保留空键和在我的 中保留 Math.max(),我只能获得最大 Salary减少类。当我将 key 保留为 EmpName 时,它会显示唯一员工的所有薪水。

我的Mapreduce代码

文件:test.csv

121,Raj,Dept1,8000
122,Kiran,Dept2,6000
123,John,Dept3,9000
public static class MyMap extends Mapper<LongWritable,Text,Text,IntWritable>    
{
public void map(LongWritable k,Text v, Context con)throws IOException, InterruptedException
{
String line = v.toString();
String[] w=line.split(",");
int sal=Integer.parseInt(w[3]);
con.write(new Text("Raj"), new IntWritable(sal));
}
}

public static class MyRed extends Reducer<Text,IntWritable,IntWritable,Text>
{
public void reduce(Text k, Iterable<IntWritable> vlist, Context con)
throws IOException , InterruptedException
{
int max=0;
for(IntWritable v:vlist)
{
max=Math.max(max, v.get());
}

con.write(new IntWritable(max), new Text());
}

输出:

9000

这里我需要的输出是:

9000 John

请告诉我如何获得此输出。

最佳答案

在您的 map 阶段,保存薪水最高的条目,并在 cleanup 期间将其写入上下文。这导致每个映射器只有一个输出,即那些映射器看到的薪水最高的条目。当您输出条目时,您可以只输出整个文本行。在您的单个 reduce 阶段,您然后再次拆分文本行并确定最大值。已发送文本行的薪水 - 并没有那么多,因为每个映射器只发送了一个项目。

Here是一个 Java 示例,用于根据用户的声誉确定前 10 名用户。你应该能从中得到灵感。

顺便说一句:您请求了代码,但没有提及使用哪种语言,也没有显示您之前的任何尝试,因此我只是将您指向上面提到的示例。

关于hadoop - 使用 MapReduce 的员工的最大工资,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38273236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com