天天看點

MapReduce中的InputFormat(2)自定義InputFormat1 概述2 運作軌迹3 自定義InputFormat

hadoop内置的輸入檔案格式類有:

1)fileinputformat<k,v>這個是基本的父類,自定義就直接使用它作為父類。

2)textinputformat<longwritable,text>這個是預設的資料格式類。key代表目前行資料距離檔案開始的距離,value代碼目前行字元串。

3)sequencefileinputformat<k,v>這個是序列檔案輸入格式,使用序列檔案可以提高效率,但是不利于檢視結果,建議在過程中使用序列檔案,最後展示可以使用可視化輸出。

4)keyvaluetextinputformat<text,text>這個是讀取以tab(也即是\t)分隔的資料,每行資料如果以\t分隔,那麼使用這個讀入,就可以自動把\t前面的當做key,後面的當做value。

5)combinefileinputformat<k,v>合并大量小資料是使用。

6)multipleinputs,多種輸入,可以為每個輸入指定邏輯處理的mapper。

2.1 mapper

進入context.nextkeyvalue()方法,進而進入wrappedmapper類。

2.2 wrappedmapper

進入該方法的nextkeyvalue(),進而進入mapcontextimpl類。

2.3 mapcontextimpl

現希望知道reader具體類型是什麼,先看reader的申明和指派。

此處看到是調用mapcontextimpl構造方法進行指派的,那麼繼續跟進看何處調用了mapcontextimpl方法。右擊mapcontextimpl > open call hierarchy。跟進一個方法叫runnewmapper可以看到,一步步看變量申明,就可以看到inputformat就是我們代碼中設定的inputformat.class類型。

基于檔案的fileinputformat的設計思想是:

a 由公共基類fileinputformat采用統一的方法,對檔案進行切分成inputsplit(如按照統一的大小)。getsplit方法。

b 由各個派生類根據自己的需求,解析inputsplit。即各個子類實作的createrecordreader方法。那麼input隻需實作自定義createrecordreader方法即可。

3.1 myinputformat

3.2 myrecordreader

3.3 testformat

參考位址:http://www.cnblogs.com/hyl8218/p/5198030.html

繼續閱讀