# Hadoop MapReduce執行過程是怎么樣的
## 一、MapReduce概述
### 1.1 什么是MapReduce
MapReduce是一種分布式計算模型,由Google提出,主要用于大規模數據集(大于1TB)的并行運算。它將復雜的、運行于大規模集群上的并行計算過程高度抽象為兩個函數:Map和Reduce。
### 1.2 MapReduce的核心思想
"分而治之"是MapReduce的核心思想:
- **Map階段**:將大數據集分解為成百上千的小數據集
- **Reduce階段**:對Map階段的中間結果進行匯總
### 1.3 MapReduce的優勢
1. 易于編程
2. 良好的擴展性
3. 高容錯性
4. 適合海量數據離線處理
## 二、MapReduce架構組成
### 2.1 主要組件
| 組件 | 功能描述 |
|------|----------|
| Client | 提交MapReduce作業 |
| JobTracker | 資源管理和作業調度 |
| TaskTracker | 執行具體任務 |
| HDFS | 存儲輸入輸出數據 |
### 2.2 角色劃分
1. **JobTracker**(主節點):
- 管理所有作業
- 調度任務到TaskTracker
- 監控任務執行
2. **TaskTracker**(從節點):
- 執行Map和Reduce任務
- 向JobTracker匯報狀態
## 三、MapReduce詳細執行流程
### 3.1 整體流程圖
```mermaid
graph TD
A[Input Data] --> B[Split]
B --> C[Map Task]
C --> D[Shuffle]
D --> E[Reduce Task]
E --> F[Output]
關鍵參數:
<property>
<name>mapreduce.input.fileinputformat.split.minsize</name>
<value>1</value>
</property>
示例代碼:
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String[] words = value.toString().split(" ");
for (String w : words) {
word.set(w);
context.write(word, one);
}
}
}
Map端Shuffle: 1. Partition:根據Reduce數量分區
public int getPartition(K key, V value, int numReduceTasks) {
return (key.hashCode() & Integer.MAX_VALUE) % numReduceTasks;
}
Reduce端Shuffle: 1. Copy:從各Map節點拉取數據 2. Merge:合并來自不同Map的數據 3. Sort:二次排序
示例代碼:
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
}
TaskTracker故障:
Task失敗:
推測執行:
<property>
<name>mapreduce.map.speculative</name>
<value>true</value>
</property>
job.setCombinerClass(WordCountReducer.class);
<property>
<name>mapreduce.map.output.compress</name>
<value>true</value>
</property>
<property>
<name>mapreduce.map.memory.mb</name>
<value>2048</value>
</property>
傳統架構 | YARN架構 |
---|---|
JobTracker | ResourceManager |
TaskTracker | NodeManager |
- | ApplicationMaster |
完整代碼示例:
public class WordCount {
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(WordCountMapper.class);
job.setCombinerClass(WordCountReducer.class);
job.setReducerClass(WordCountReducer.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);
}
}
數據傾斜解決方案:
Shuffle優化:
輸入文件處理:
參數調優:
<!-- 設置Map任務數 -->
<property>
<name>mapreduce.job.maps</name>
<value>100</value>
</property>
監控工具:
MapReduce作為Hadoop的核心計算框架,雖然在新一代計算框架面前顯得效率不足,但其”分而治之”的思想仍然深刻影響著大數據處理領域。理解MapReduce的執行原理,不僅有助于優化傳統Hadoop作業,更能為學習其他分布式計算框架奠定堅實基礎。 “`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。