Java 使用mapreduce的第二大薪资-输出不符合预期
我写了一个小的mapreduce作业来寻找数据集中第二高的薪水。我相信第二高工资逻辑是正确的。但是我得到了多个输出,这是不正确的,应该只有一个名为John,9000的输出。而且输出也不正确,这里我给出了数据集和代码Java 使用mapreduce的第二大薪资-输出不符合预期,java,hadoop,mapreduce,hadoop2,Java,Hadoop,Mapreduce,Hadoop2,我写了一个小的mapreduce作业来寻找数据集中第二高的薪水。我相信第二高工资逻辑是正确的。但是我得到了多个输出,这是不正确的,应该只有一个名为John,9000的输出。而且输出也不正确,这里我给出了数据集和代码 hh,0,Jeet,3000 hk,1,Mayukh,4000 nn,2,Antara,3500 mm,3,Shubu,6000 ii,4,Parsi,8000 输出应该是Shubu,6000,但是我得到的是下面的输出 Antara -2147483648 May
hh,0,Jeet,3000
hk,1,Mayukh,4000
nn,2,Antara,3500
mm,3,Shubu,6000
ii,4,Parsi,8000
输出应该是Shubu,6000,但是我得到的是下面的输出
Antara -2147483648
Mayukh -2147483648
Parsi 3500
Shubu 4000
我使用的代码是
public class SecondHigestMapper extends Mapper<LongWritable,Text,Text,Text>{
private Text salary = new Text();
private Text name = new Text();
public void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException{
if(key.get()!=0){
String split[]= value.toString().split(",");
salary.set(split[2]+";"+split[3]);
name.set("ignore");
context.write(name,salary);
}
}
}
public class SecondHigestReducer extends Reducer<Text,Text,Text,IntWritable>{
public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
int highest = 0;
int second_highest = 0;
int salary;
for(Text val:values){
String[] fn = val.toString().split("\\;");
salary = Integer.parseInt(fn[3]);
if(highest < salary){
second_highest = highest;
highest =salary;
} else if(second_highest < salary){
second_highest = salary;
}
}
String seconHigest = String.valueOf(second_highest);
context.write(new Text(key),new Text(seconHigest));
}
}
public class SecondHigestDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = new Job(conf,"Second Higest Sal");
job.setJarByClass(SecondHigestDriver.class);
job.setMapperClass(SecondHigestMapper.class);
job.setCombinerClass(SecondHigestReducer.class);
job.setReducerClass(SecondHigestReducer.class);
job.setOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
请帮我解决这个问题使用一个键将所有工资强制放入一个减速机
name.set("ignore"); // Could use a NullWritable
salary.set(split[2]+";"+split[3])); // change to TextWritable
context.write(name,salary); // need to change the signature of the mapper class
然后在reducer中,将方法更改为接受文本值,然后将它们分开,转换工资,然后比较这些值如果您为“第二高”逻辑编写单元测试将有所帮助。。。在任何情况下,你的名字都会被发送到不同的减薪者那里,因此你永远不会只获得第二高的工资,而是拥有相同姓名的人的第二高工资。我尝试了你的解决方案并编辑了我的问题。你能检查一下我为什么会得到这个例外吗?@Mandrek你真的不应该以这种方式编辑你的问题,现在完全不同了。板球队007回答了你的问题,很可能是正确的。如果你有不同的问题,你应该接受他的回答并问一个新问题。你现在遇到的问题很常见,快速搜索可能会找到可能的答案。要给您一个指针,请尝试删除
job.setCombinerClass(SecondHigestReducer.class)代码>从你的司机那里。@BinaryNerd我很高兴sorry@Mandrek您应该会看到数组越界异常<代码>fn[3]
在一个值周围只有两个值时是不可能的semicolon@cricket_007你能编辑我的代码吗?这会很有帮助的?事实上,我是hadoop新手
name.set("ignore"); // Could use a NullWritable
salary.set(split[2]+";"+split[3])); // change to TextWritable
context.write(name,salary); // need to change the signature of the mapper class