在我的Reduce类中,我需要多个输出。。。我正在使用MapReduceBase。如何在我的配置方法中初始化我的多路输出实例(即out)?由于我无法初始化,我得到了null指针异常。。。请帮帮我…这是我的密码
public static class Reduce extends MapReduceBase implements
Reducer<Text, Text, NullWritable, Text> {
private MultipleOutputs<NullWritable, Text> out;
public void configure(JobConf job) {
}
public void reduce(Text key, Iterator<Text> values,
OutputCollector<NullWritable, Text> output, Reporter reporter)
throws IOException {
while (values.hasNext()) {
try {
out.write(NullWritable.get(), values.next(), "outoutPath/"
+ key.toString());//Null pointer exception
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
这里有一个正确的例子。
public class ReducerToFileSystem extends Reducer<Text, Text, Text, Text>
{
private MultipleOutputs<Text, Text> mos;
public void setup(Context context){
mos = new MultipleOutputs<Text, Text>(context);
}
//public void reduce(Text key, Text value, Context context)
//throws IOException, InterruptedException (This was the mistake, changed the signature and it worked fine)
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException
{
//context.write(key, value);
mos.write("cdb1", key, value, OUTPUT_DIR+"/"+"cdb1");
mos.write("cdb2", key, value, OUTPUT_DIR+"/"+"cdb2");
context.progress();
}
public void cleanup(Context context) throws IOException, InterruptedException {
mos.close();
}
}