2013-01-08 53 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