Hadoop 映射减少链

Hadoop 映射减少链,hadoop,mapreduce,Hadoop,Mapreduce,可能重复: 我有地图减少链如下所述 Job1(Map1->reduce1)-->Job2(Map2,Reduce2)Job1.waitForCompletion(true) 我需要Map2中的一个值(假设int a,由Reduce 1创建) 我该怎么做??请分享您的想法您可以使用ChainMapper和ChainReducer。下面是一个示例代码供您帮助 注意: the out-put Type of key-value derive which Mapper and reducer

可能重复:

我有地图减少链如下所述

Job1(Map1->reduce1)-->Job2(Map2,Reduce2)Job1.waitForCompletion(true)

我需要Map2中的一个值(假设int a,由Reduce 1创建)


我该怎么做??请分享您的想法

您可以使用ChainMapper和ChainReducer。下面是一个示例代码供您帮助





注意:

the out-put Type of  key-value derive which Mapper and reducer is to be called next so , the output Type of Map1 should me same as Input Type of key-value of Reduce1 AND the output Type of Reduce1 should me same as Input Type of key-value of Map2 and 
the output Type of Map2 should me same as Input Type of key-value of Reduce2

解决问题的计数器
您可以使用Job1中的计数器Reduce1从Job1中获取值,然后将该值传递给Job2

1.使用计数器设置值的示例代码

    Reducer()
{
public static enum COUNTER {
  INTVALUE
};

Reduce()
{
// Old API
reporter.incrCounter(COUNTER .INTVALUE, 1);

//NEW API
context.getCounter(COUNTER .INTVALUE).increment(1);

}

}


2.从job1获取设置的计数器,然后将其设置为Job2的JonConf,映射程序可以从中获取相同的值。




3.Map2从Job1计数器获取值->Jobconf2


mapper()
{
int值;
@凌驾
公共无效配置(JobConf作业){
value=job.getInt(“名称”,0);
}
@凌驾
公共无效映射(文本键、文本值、,
OutputCollector输出,报告器arg3)
抛出IOException{
}
}
另一个ans
将Reduce1的输出保存为平面文件(hdfs) 在第二个作业中设置驱动程序(作业)时读取该文件。然后在上下文中设置变量。


在mapper中(map2)

mapper()
{
int值;
@凌驾
公共无效配置(JobConf作业){
value=job.getInt(“名称”,0);
}
@凌驾
公共无效映射(文本键、文本值、,
OutputCollector输出,报告器arg3)
抛出IOException{
}
}


是的,但他想在两个作业之间共享临时数据。我还需要在Map2中处理一个输入文件。所以我需要一个来自HDFS和Reduce1输出的输入文件,作为map2的输入。我添加了另一种方法来做同样的事情。您的代码示例只使用ChainReducer,没有ChainMapper,只是一个复制粘贴错误。也请考虑回答两次。这使得评论和评论更容易vote@rretzbach:编辑并发布了两个答案,请查看、评论和投票其他答案。泰。
    Reducer()
{
public static enum COUNTER {
  INTVALUE
};

Reduce()
{
// Old API
reporter.incrCounter(COUNTER .INTVALUE, 1);

//NEW API
context.getCounter(COUNTER .INTVALUE).increment(1);

}

}
main()
{
// .....
jobclient1.submit(job1);
RunningJob job = JobClient1.runJob(conf);  // blocks until job completes
Counters c = job.getCounters();
int value= c.getCounter(COUNTER .INTVALUE);

// Now set the value in Job2 
Job job2 = new JobConf(conf);
job2.setInt("name", value);
}
    mapper()
 {
    int value;

    @Override
    public void configure(JobConf job) {

        value=job.getInt("name", 0);
    }

    @Override
    public void map(Text key, Text value,
            OutputCollector<LongWritable, Text> output, Reporter arg3)
            throws IOException {


    }
}
----------
//read reducer output from file . and set it @name variable 
Configuration conf = getConf();
Job job = new JobConf(conf);
conf.setInt("name", 0000);
    mapper()
{
int value;

        @Override
        public void configure(JobConf job) {

            value=job.getInt("name", 0);
        }

        @Override
        public void map(Text key, Text value,
                OutputCollector<LongWritable, Text> output, Reporter arg3)
                throws IOException {


        }
}