# 如何编写MapReduce程序 ## 1. MapReduce概述 ### 1.1 什么是MapReduce MapReduce是一种分布式计算编程模型,由Google在2004年提出,主要用于大规模数据集(大于1TB)的并行运算。其核心思想是将计算过程分解为两个主要阶段: - **Map阶段**:对输入数据进行分割和处理 - **Reduce阶段**:对Map结果进行汇总 ### 1.2 工作原理 1. 输入数据被自动分割成固定大小的块(通常64MB或128MB) 2. Master节点将Map任务分配给Worker节点 3. Map任务处理输入数据并生成中间键值对 4. 系统对中间结果进行排序和分组 5. Reduce任务处理分组后的数据 6. 最终结果写入分布式文件系统 ### 1.3 适用场景 - 大规模日志分析 - 网页索引构建 - 数据挖掘 - 机器学习特征提取 ## 2. 开发环境搭建 ### 2.1 基础环境要求 - Java JDK 1.8+ - Hadoop 2.7+(推荐3.x版本) - Maven(项目管理工具) - IDE(IntelliJ IDEA或Eclipse) ### 2.2 Hadoop安装配置 ```bash # 下载Hadoop wget https://archive.apache.org/dist/hadoop/core/hadoop-3.3.1/hadoop-3.3.1.tar.gz # 解压并配置环境变量 export HADOOP_HOME=/path/to/hadoop export PATH=$PATH:$HADOOP_HOME/bin <dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-core</artifactId> <version>3.3.1</version> </dependency> </dependencies> | 组件 | 职责 |
|---|---|
| InputFormat | 定义输入数据格式和分割方式 |
| Mapper | 实现map()方法处理输入记录 |
| Partitioner | 决定中间结果的Reduce节点分配 |
| Reducer | 实现reduce()方法汇总结果 |
| OutputFormat | 定义输出数据格式 |
原始数据 → InputSplit → RecordReader → Mapper → Partitioner → Shuffle & Sort → Reducer → RecordWriter → 输出文件 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); } } Mapper类:
Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>map()方法处理每条记录Reducer类:
Reducer<KEYIN, VALUEIN, KEYOUT, VALUEOUT>reduce()方法汇总相同键的值Driver程序:
Combiner是本地Reduce操作,可减少网络传输:
job.setCombinerClass(IntSumReducer.class); 实现数据均衡分布:
public class CustomPartitioner extends Partitioner<Text, IntWritable> { @Override public int getPartition(Text key, IntWritable value, int numPartitions) { // 按首字母分区 return key.toString().charAt(0) % numPartitions; } } 实现Writable接口:
public class WebLogRecord implements Writable { private Text ip; private LongWritable timestamp; // 实现write()和readFields()方法 @Override public void write(DataOutput out) throws IOException { ip.write(out); timestamp.write(out); } @Override public void readFields(DataInput in) throws IOException { ip.readFields(in); timestamp.readFields(in); } } | 参数 | 默认值 | 建议值 | 说明 |
|---|---|---|---|
| mapreduce.task.io.sort.mb | 100 | 200 | 排序缓冲区大小 |
| mapreduce.map.sort.spill.percent | 0.8 | 0.9 | 溢出比例 |
| mapreduce.reduce.shuffle.parallelcopies | 5 | 20 | 并行拷贝数 |
输入文件处理:
内存配置:
<!-- mapred-site.xml --> <property> <name>mapreduce.map.memory.mb</name> <value>2048</value> </property> 压缩中间结果:
conf.set("mapreduce.map.output.compress", "true"); conf.set("mapreduce.map.output.compress.codec", "org.apache.hadoop.io.compress.SnappyCodec"); Configuration conf = new Configuration(); conf.set("mapreduce.framework.name", "local"); conf.set("fs.defaultFS", "file:///"); 查看任务日志:
yarn logs -applicationId <app_id> 内存溢出:
数据倾斜:
任务超时:
<property> <name>mapreduce.task.timeout</name> <value>600000</value> </property> 分析Nginx日志统计: - 每个URL的访问量 - 每个IP的访问频率 - 高峰时段统计
192.168.1.1 - - [10/Oct/2023:14:32:01 +0800] "GET /index.html HTTP/1.1" 200 2326 public class LogMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private Text url = new Text(); private final static IntWritable one = new IntWritable(1); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] parts = line.split(" "); if(parts.length > 6) { url.set(parts[6]); // 提取URL context.write(url, one); } } } | 框架 | 特点 | 适用场景 |
|---|---|---|
| Spark | 内存计算 | 迭代算法 |
| Flink | 流批一体 | 实时处理 |
| Hive | SQL接口 | 数据仓库 |
注意:实际运行MapReduce程序前,需确保Hadoop集群已正确配置。建议先在伪分布式环境下测试,再部署到生产集群。 “`
(全文约4200字,包含代码示例、配置参数和实用技巧)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。