题目内容
编程题6:使用并行计算框架MapReduce实现TopN,按要求如下补充代码。......public class TopNMapper extends Mapper{private TreeMap repToRecordMap = new TreeMap();@Overrideprotected void map(LongWritable key, Text value,Context context){String line = value.(1)();String[] nums = line.split(" ");for(String num :nums){repToRecordMap.put(Integer.parseInt(num), " ");if((2).size()>5){repToRecordMap.remove(repToRecordMap.(3));}}}@Overrideprotected void cleanup(Context context){for(Integer i:repToRecordMap.keySet()){try {(4).write(NullWritable.get(), new IntWritable(i));} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.(5)();}}}}public class TopNReducer extends Reducer {private TreeMap repToRecordMap = (6)TreeMap(new Comparator() {public int (7)(Integer a,Integer b){return b-a;}});public void reduce(NullWritable key, Iterable values, Context context) throws IOException, InterruptedException{for((8)value:values){repToRecordMap.put(value.get(), " ");if(repToRecordMap.size()>5) {repToRecordMap.(9)(repToRecordMap.firstKey());}}for(Integer i:repToRecordMap.keySet()){context.write(NullWritable.get(),(10));}}}public class TopNRunner {public static void main(String[] args) throws Exception {Configuration conf = new Configuration();Job job = Job.getInstance(conf);job.setJarByClass((11));job.setMapperClass(TopNMapper.class);job.setReducerClass(TopNReducer.class);job.(12)(1);job.setMapOutputKeyClass(NullWritable.class);job.setMapOutputValueClass(IntWritable.class);job.setOutputKeyClass(NullWritable.class);job.setOutputValueClass(IntWritable.class);FileInputFormat.setInputPaths(job,new Path("/input"));(13).setOutputPath(job,new Path("/output"));boolean res = job.(14)(true);System.(15)(res?0:1);}}
查看答案
搜索结果不匹配?点我反馈