Mapreduce的輸入格式

2021-06-14 16:17:53 字數 2764 閱讀 8083

map(k1,v1)——>list(k2,v2)

reduce(k2,list(v2))->list(k3,v3)

reduce的輸入型別必須與map函式的輸出型別相同

combine的輸入輸出鍵值型別必須相同,也就是k2,v2

} static class reducer extends reudcer }

partion 函式將中間的鍵值對進行處理,並且返回乙個分割槽索引。實際上分割槽由鍵單獨決定,值是被忽略的。

public class hashpationimplments partioner

public int getpartion(k2 key, v2 value, int numpartions) }

mr的二次排序

乙個輸入分片就是由單個map處理的輸入塊。每乙個map操作只處理乙個輸入分片。每個分片被劃分為若干記錄,每條記錄就是乙個鍵值對。

public inte***ce 

inputsplit extends writable

mapreduce應用開發人員不需要直接處理inputsplit,因為它是由inputformat建立的,inputformat負責產生輸入分片並將他們分割成記錄。

public inte***ce 

inputformat

map任務用

recordreader來生成記錄的鍵值對(

下面這幾個函式都是比較重要的要記住)

k key=reader.createkey();

v value = reader.createvalue();

while(reader.next(key,value))

fileinputformat類

控制分片大小的屬性

mapred.min.split.size  檔案分片最小有效位元組數

mapred.max.split.size  最大有效位元組數

dfs.blokc.size hdfs中塊的大小 

combinefileinputformat

決定哪些分片放入同乙個分片時,combineinputformat會考慮到節點和機架的因素,所以,在典型mapreduce作業處理輸入的速度不會下降。

filesystem fs = filesystem.get(uri.create(uri),conf);

path paths = new path[args.lengths];

for(int i = 0;i

filestatus status = fs.liststatus(pahts);

path listedpaths= fileutil.stat2paths(status);

for(path p : listedpaths)

if (file.isfile()) // 是目錄的情況

else }

檔案輸入分片的屬性

map.input.file   輸入檔案的路徑 string filename  = 

conf.get("map.input.file"); 獲得輸入檔案的名字

map.input.start  分片開始處的位元組便宜量

map.input.length    分片的長度

文字輸入 textinputformat

textinputformat是預設的inputformat,每條記錄是一行輸入,鍵是longwritable型別,是該行在檔案中的位元組偏移量(

很難取得行號,因為檔案是按位元組而不是按行切分為分片)。值是這行的內容。

keyvaluetextinputformat 對於一行就包含了key和value的情況很實用。

可以通過

key.value.separator.in.input.line屬性來指定分隔符。選定命令用如下對命令來設定。

通過mapred.line.input.format.linespermap 屬性來設定n的值

hadoop提供了streamxmlrecordreader類,在org.apache.haodoop.streaming包中,通過把輸入格式設定成streaminputformat,把stream.recordreader.class屬性設定為org.apache.haodoop.streaming.streamxmlrecorderreader來使用streamxmlrecordreader類。

二進位制輸入

sequencefileinputformat  

對於多種輸入

對於多種輸入  使用multipleinputs.addinputpath(conf,

ncdcinputpaht,textinputformat.class, 

multipleinputs.addinputpath(conf,

metofficeinputpath,textinputformat.class,

資料庫輸入使用的是dbinputformat 專業工具是sqoop

partitioner是partitioner的基類,如果需要定製partitioner也需要繼承該類。         

該類有乙個比較重要的函式就是int getpartion(k key,v value,int numpartions)         返回要分的個數 

MapReduce提供的輸入輸出格式

輸入格式 key value textinputformat 預設格式,讀取檔案的行 行的位元組偏移量 行的內容 keyvalueinputformat 把行解析為鍵值對 第乙個tab字元前的所有字元 行剩下的內容 sequencefileinputformat hadoop定義的高效能二進位制格式...

MapReduce的型別和格式

context類物件用於輸出鍵 值對 map k1,v1 list k2,v2 combiner k2,list v2 list k2,v2 reduce k2,list v2 list k3,v3 partition函式對中間結果的鍵值對 k2 v2 進行處理,並返回乙個分割槽索引 partiti...

mapreduce的型別與格式

1 預設的mapreduce作業 預設的輸入格式是textinputformat 預設的partitioner是hashpartitioner 預設的reducer是reducer 預設情況下,只有乙個reducer 沒有設定map任務的數量,原因是該數量等於輸入檔案被劃分成的分塊數,取決於輸入檔案...