我寫了一個小的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
提交
取消