Java MapReduce中的全局变量或属性?
我希望能够在MR作业的映射阶段设置某种变量或标志,以便在作业完成后检查。我认为用一些代码来演示我想要的最好的方法是:p.s我正在使用Hadoop 2.2.0Java MapReduce中的全局变量或属性?,java,hadoop,hadoop2,Java,Hadoop,Hadoop2,我希望能够在MR作业的映射阶段设置某种变量或标志,以便在作业完成后检查。我认为用一些代码来演示我想要的最好的方法是:p.s我正在使用Hadoop 2.2.0 public class MRJob { public static class MapperTest extends Mapper<Object, Text, Text, IntWritable>{ public void map(Object key, Text value, Context
public class MRJob {
public static class MapperTest
extends Mapper<Object, Text, Text, IntWritable>{
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
//Do some computation to get new value and key
...
//Check if new value equal to some condition e.g if(value < 1) set global variable to true
context.write(newKey, newValue);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(new Configuration(), "word_count");
//set job configs
job.waitForCompletion(true);
//Here I want to be able to check if my global variable has been set to true by any one of the mappers
}
}
公共类MRJob{
公共静态类MapperTest
扩展映射器{
公共无效映射(对象键、文本值、上下文
)抛出IOException、InterruptedException{
//进行一些计算以获得新的值和键
...
//检查新值是否等于某些条件,例如(值<1)是否将全局变量设置为true
write(newKey,newValue);
}
}
公共静态void main(字符串[]args)引发异常{
Configuration conf=新配置();
Job Job=Job.getInstance(新配置(),“字数”);
//设置作业配置
job.waitForCompletion(true);
//在这里,我希望能够检查我的全局变量是否被任何一个映射器设置为true
}
}
为此,请使用计数器
public static enum UpdateCounter {
UPDATED
}
@Override
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
if(value < 1) {
context.getCounter(UpdateCounter.UPDATED).increment(1);
}
context.write(newKey, newValue);
}
谢谢你的回答。我只是想澄清一下,我是否在mapper类中创建了UpdateCounter?@Mo。只要在运行时可以访问该类,在何处创建UpdateCounter并不重要。对不起,最后一个问题。我有一个稍微相似的用例,需要设置一个自定义全局值。e、 g.在映射程序中,类似于:map(key,…){if(key==“foo”){globalVariable=value;}
I然后需要在作业完成后访问该变量,类似于计数器。Thanks@Mo.然后将一个文件写入HDFS,没有内置的工具轻松实现这一点。
Configuration conf = new Configuration();
Job job = Job.getInstance(new Configuration(), "word_count");
//set job configs
job.waitForCompletion(true);
long counter = job.getCounters().findCounter(UpdateCounter.UPDATED).getValue();
if(counter > 0)
// some mapper has seen the condition