2013-01-08 52 views
12

我認爲他們指的是減速,但在我的節目,我有job.setOutputKeyClass和job.setOutputReduceClass指向哪裏?

public static class MyMapper extends Mapper< LongWritable, Text, Text, Text >

public static class MyReducer extends Reducer< Text, Text, NullWritable, Text >

所以,如果我有

job.setOutputKeyClass(NullWritable.class);

job.setOutputValueClass(Text.class);

我得到以下異常

Type mismatch in key from map: expected org.apache.hadoop.io.NullWritable, recieved org.apache.hadoop.io.Text

但如果我有

job.setOutputKeyClass(Text.class);

是沒有問題的。

有沒有與我的代碼錯誤或這是因爲NullWritable或其他?

我也必須使用job.setInputFormatClassjob.setOutputFormatClass?因爲我的程序沒有它們正確運行。

回答

28

調用job.setOutputKeyClass(NullWritable.class);將設置預期的類型作爲來自map和reduce階段的輸出。

如果您的映射器發射的類型與Reducer不同,您可以使用JobConfsetMapOutputKeyClass()setMapOutputValueClass()方法設置映射器發出的類型。這些隱式設置了Reducer預期的輸入類型。

(來源:Yahoo Developer Tutorial

關於你提到的第二個問題,默認InputFormatTextInputFormat。這將每個輸入文件的每一行視爲單獨的記錄,並且不執行解析。如果你需要處理你的輸入不同的格式,你可以調用這些方法,下面是一些例子:

InputFormat    | Description          | Key          | Value 
-------------------------------------------------------------------------------------------------------------------------------------------------------- 
TextInputFormat   | Default format; reads lines of text files  | The byte offset of the line    | The line contents 
KeyValueInputFormat  | Parses lines into key, val pairs     | Everything up to the first tab character | The remainder of the line 
SequenceFileInputFormat | A Hadoop-specific high-performance binary format | user-defined        | user-defined 

OutputFormat默認實例是TextOutputFormat,它寫在一個單獨的線(鍵,值)對文本文件。下面的一些例子:

OutputFormat    | Description 
--------------------------------------------------------------------------------------------------------- 
TextOutputFormat   | Default; writes lines in "key \t value" form 
SequenceFileOutputFormat | Writes binary files suitable for reading into subsequent MapReduce jobs 
NullOutputFormat   | Disregards its inputs 

(來源:Other Yahoo Developer Tutorial

+0

哦,你是對的,我不知道你runs.Of mentioned.Adding他們我的計劃的兩種方法當然,我用工作不是沒JobConf,但方法也存在。非常感謝!你能告訴我關於我問題的最後部分嗎? – nik686

+0

@ nik686我在上面的問題的最後部分添加了答案。 –

+0

所以默認是TextInputFormat和TextOutputFormat,這就是我的程序運行的原因。非常感謝! – nik686