Hadoop MapReduce

將會按照HDFS ,MapReduce, Yarn的順序更新, 近期還會整理Zookeper, Hive, pig等相關

如果對您有幫助或者解決了您的問題, 就幫我點個贊或者評論關注支持吧, 您的鼓勵是我寫博客的最大支持, 感謝!

目前是大四研一實習, 單位比較人性化週末雙休所以就斷更兩天了. 今天繼續更新

0. MapReduce是什麼?

MapReduce是一種編程模型,用於大規模數據集(大於1TB)的並行運算。MapReduce將分成兩個部分"Map(映射)"和"Reduce(歸約)"。

思想是運算跟着數據走, 數據在哪我就在哪運算. 因爲移動數據成本太大

1. MapReduce工作過程拆解

Demo

下面通過一個景點案例(單詞統計)看MapReduce是如何工作的。

有一個文本文件,被分成了4份,分別放到了4臺服務器中存儲

Text1:the weather is good

Text2:today is good

Text3:good weather is good

Text4:today has good weather

現在要統計出每個單詞的出現次數。

1.1處理過程

1.1.1 Map拆分單詞

  • map節點1
    • 輸入:“the weather is good”
    • 輸出:(the,1),(weather,1),(is,1),(good,1)

  • map節點2
    • 輸入:“today is good”
    • 輸出:(today,1),(is,1),(good,1)

map3,4,5.......

1.1.2 Map合併統計單詞

  • map節點1

  • map節點2

map3,4,5.......

1.1.3 Reduce彙總統計

2. MapReduce 編程思路

map - 對於傳入的單行數據處理

reduce - 對於傳進來的map處理

 

3. MapReduce JAR包講解

3.1 Map程序

import java.io.IOException;
import org.apache.hadoop.io.Intwritable;
import org.apache.hadoop.io.Longwritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
public class Wordcountmapper extends Mapper<LongWritabIe, Text,
    Text, Intwritabte> {
    @OverrIde
    protected void map(LongWritabIe key, Text value, Context context )
    throws IOException, InterruptedException {
    //得到每一行數據
    String line = value. tostring();
    String[] words = line.split(" ");
    for (String word : words) {
        context.write(new Text(word), new Intwritable(l));
        }
    }
}

其中的4個類型分別是:輸入key類型、輸入value類型、輸出key類型、輸出value類型

Mapper<LongWritable, Text, Text, IntWritable>

 

輸入key默認是mr所讀到一行文本的起始偏移量(Long類型)

輸入value默認是mr所讀到的一行的數據內容(String類型)

輸出key 和value則是用戶自定義的

因爲MapReduce程序的輸出數據需要在不同機器間傳輸,所以必須是可序列化的,例如Long類型,Hadoop中定義了自己的可序列化類型LongWritable,String對應的是Text,int對應的是IntWritable。

3.2 Reduce程序

import java.io.IOException;
import org.apache.hadoop.io.Intwritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WordCountReducer extends Reducer<Text, Intwritable,
    Text, Intwritable> {
    @Overr1de
    protected void reduce(Text key, Interable<Intwritable>values, 
    Context context) throws IOException, InterruptedException {
        Integer count = 0;
        for (Intwritable value : values) {
            count +=value.get();
            context.write( key, new Intwritable(count)) ;
        }
   }
}

和之前一樣分別指:輸入key的類型、輸入value的類型、輸出key的類型、輸出value的類型。

Reducer<Text, IntWritable, Text, IntWritable>

 

reduce方法接收的是:一個字符串類型的key、一個可迭代的數據集

比如

reduce任務讀取到map任務處理結果是這樣的:

(good,1)(good,1)(good,1)(good,1)

當傳給reduce方法時,就變爲:

key:good
value:(1,1,1,1)

 

四 MapReduce的執行過程

1. 客戶端提交任務

查看文件規模, 形成任務規劃分配

2. 啓動 appmaster

appmaster負責maptask和reducetask的啓動、監控、協調管理工作。

yarn找一個合適的服務器來啓動appmaster,並把job.split、jar、xml交給它

3. 啓動maptask

根據固化文件job.split中的分片信息啓動maptask,一個分片對應一個maptask。

4. 執行maptask

讀一行就調一次map方法.結果保存到本機的一個結果文件

5. 啓動ReduceTask

maptask的結果中有幾個分區就啓動幾個reducetask

6. 執行reduceTask

reducetask把讀到的數據按key組織好,傳給reduce方法進行處理,處理結果寫到指定的輸出路徑。

 

 

發佈了40 篇原創文章 · 獲贊 221 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章