job.setOutputKeyClass和job.setOutputReduceClass在哪里引用?

我认为他们指的是减速机,但在我的程序中我有

public static class MyMapper extends Mapper

public static class MyReducer extends Reducer

所以,如果我有

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吗? 因为没有它们我的程序运行正常。

调用job.setOutputKeyClass( NullWritable.class ); 将设置预期的类型作为map和reduce阶段的输出。

如果Mapper发出的类型与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 

(来源: 其他Yahoo开发人员教程 )