题目内容
编程题4:使用并行计算框架MapReduce实现单词统计,按要求如下补充代码。......// WordCountMapper类public class WordCountMapper extends Mapper {@Overrideprotected void map(LongWritable key, Text value, Context context) throws Exception {// 数据类型转换为StringString line = value.(1)();// 行内容按照分隔符逗号切割String[] words = line.split((2));// 遍历数组,标记<单词,1>for (String word : words) {// Map阶段处理的数据发送给Reduce阶段作为输入数据context.(3)(new Text(word), new IntWritable(1));}}}......// WordCountReducer类public class WordCountReducer extends Reducer {@Overrideprotected void reduce(Text key, Iterable value, Context context) throws Exception {//定义一个计数器int count = 0;//遍历一组迭代器,累加单词的总次数for ((4) iw : value) {count += iw.get();}// 把Reduce阶段处理的数据作为输出数据context.write(key, new IntWritable((5)));}}......// WordCount类......public class WordCountDriver {public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {Configuration conf = new (6)();// 创建配置对象Job job = Job.getInstance(conf);// 创建带配置的Job作业对象job.setJarByClass(WordCountDriver.class);// 装载主类job.(7)(WordCountMapper.class);// 装载WordCountMapperjob.setReducerClass(WordCountReducer.class);// 装载WordCountReducer//装载map阶段的输入类job.setMapOutputKeyClass((8));job.setMapOutputValueClass(IntWritable.class);//装载reducer阶段输出类job.(9)(Text.class);job.setOutputValueClass(IntWritable.class);FileInputFormat.setInputPaths(job, "/input");// 指定源路径FileOutputFormat.setOutputPath(job, new Path("/output"));// 指定目标路径// 提交jobboolean res =(10).waitForCompletion(true);System.out.println(res ? "job完成!" : "job未完成!");}}
查看答案
搜索结果不匹配?点我反馈
更多问题