# Hadoop中MapReduce的示例分析 ## 一、MapReduce概述 ### 1.1 基本概念 MapReduce是Google提出的分布式计算模型,后由Apache Hadoop实现并开源。其核心思想是将大规模数据处理任务分解为两个阶段: - **Map阶段**:对输入数据进行分割和初步处理 - **Reduce阶段**:对Map结果进行汇总和聚合 ### 1.2 编程模型特点 - 自动并行化处理 - 容错机制(自动重新执行失败任务) - 数据本地化优化 - 适合批处理场景 ## 二、经典WordCount示例解析 ### 2.1 问题描述 统计文本文件中每个单词出现的频率,这是MapReduce的"Hello World"程序。 ### 2.2 Java实现代码 ```java public class WordCount { // Mapper实现 public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } } // Reducer实现 public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context ) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } // 主驱动程序 public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
构建文档检索系统,建立”单词->文档列表”的映射关系。
public class InvertedIndex { public static class InvertedIndexMapper extends Mapper<Object, Text, Text, Text> { private Text word = new Text(); private Text docId = new Text(); protected void setup(Context context) { // 获取输入文件名称作为文档ID String filename = ((FileSplit)context.getInputSplit()).getPath().getName(); docId.set(filename); } public void map(Object key, Text value, Context context) { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, docId); } } } public static class InvertedIndexReducer extends Reducer<Text, Text, Text, Text> { public void reduce(Text key, Iterable<Text> values, Context context) { Set<String> docSet = new HashSet<>(); for (Text val : values) { docSet.add(val.toString()); } context.write(key, new Text(String.join(",", docSet))); } } }
参数 | 默认值 | 建议值 | 说明 |
---|---|---|---|
mapreduce.task.io.sort.mb | 100MB | 200-400MB | 内存缓冲区大小 |
mapreduce.map.sort.spill.percent | 0.8 | 0.9 | 溢出阈值 |
mapreduce.reduce.shuffle.parallelcopies | 5 | 10-20 | 并行拷贝数 |
特性 | 旧API(org.apache.hadoop.mapred) | 新API(org.apache.hadoop.mapreduce) |
---|---|---|
基类 | 实现Mapper/Reducer接口 | 继承Mapper/Reducer基类 |
配置方式 | JobConf | Configuration |
上下文对象 | OutputCollector | Context |
// 统计用户点击商品类目的分布 public class UserBehaviorAnalysis { public static class UserBehaviorMapper extends Mapper<LongWritable, Text, Text, IntWritable> { public void map(LongWritable key, Text value, Context context) { // 解析日志字段:user_id|item_id|category_id|behavior|timestamp String[] fields = value.toString().split("\\|"); if(fields[3].equals("click")) { context.write(new Text(fields[0]+":"+fields[2]), new IntWritable(1)); } } } public static class UserBehaviorReducer extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) { int sum = 0; for(IntWritable val : values) { sum += val.get(); } context.write(key, new IntWritable(sum)); } } }
job.setNumReduceTasks(0)
虽然Spark等新技术兴起,但MapReduce在以下场景仍不可替代: - 超大规模批处理 - 与HDFS深度集成的场景 - 需要严格保证处理顺序的场景
本文通过典型示例详细解析了MapReduce编程模型的核心机制,并提供了实际开发中的优化建议。随着大数据生态的发展,理解MapReduce原理仍然是构建分布式系统的坚实基础。 “`
注:实际使用时需要: 1. 替换示例图片URL 2. 根据具体Hadoop版本调整API细节 3. 补充实际案例数据 4. 扩展性能优化章节的具体测试数据 5. 添加参考文献和延伸阅读建议
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。