简体   繁体   中英

Initialize multipleoutputs instance in configure method in MapReduceBase

In my Reduce class i need multiple outputs... Im using MapReduceBase. How to initialize my multipleoutputs instance(ie out) in my configure method? Since i am not able to initialize im getting null pointer exception... Please help me... here is my code

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();
            }
        }
    }
}

Here is an right example.

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();
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM