java - 在MapReduceBase中的configure方法中初始化多输出实例

标签 java hadoop mapreduce

在我的Reduce类中,我需要多个输出...我正在使用MapReduceBase。如何在我的configure方法中初始化我的multioutputs实例(即out)?由于我无法初始化我正在获取空指针异常...请帮助我...这是我的代码

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

关于java - 在MapReduceBase中的configure方法中初始化多输出实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30979361/

相关文章:

java - 从 Tiles View (JSP) 访问 Spring bean

java - 如何显示按钮并在用户按下按钮时显示随机数?

java - Hibernate 不断在不同位置寻找配置文件

java - 无法运行HBase的示例-权威指南

amazon-web-services - AWS 上的 Hadoop 提供 "java.net.ConnectException Connect refused"

java - Hadoop 查找任务尝试的主机名

java - 安卓;如何在执行方法时显示按钮

hadoop - 声明 hivevar 时出现 Hive 错误

amazon-ec2 - EC2上的Spark无法利用所有可用内核

regex - 正则表达式模式困惑