我写了一个小的mapreduce作业来查找数据集中第二高的薪水。我相信第二个最高薪水逻辑是正确的。但是我得到了多个不正确的输出,应该只有一个名称为John的输出,例如9000。而且输出也不正确,这里我给出了数据集和代码hh,0,Jeet,3000hk,1,Mayukh,4000nn,2,Antara,3500mm,3,Shubu,6000ii,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));} }
1 回答
MYYA
TA贡献1868条经验 获得超4个赞
使用单个键将所有薪水强制到一个减速器中
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
然后在化简器中,将方法更改为接受文本值,然后将其拆分,转换薪水,然后进行比较
添加回答
举报
0/150
提交
取消