我正在使用Hadoop制作一个mapreduce项目。目前我有3个顺序作业。
我想要使用Hadoop计数器,但问题是我想在第一个作业中进行实际计数,然后在第三个作业的reducer中访问计数器值。
我该如何实现这个功能?我应该在哪里定义enum
?我需要通过第二个作业传递它吗?如果能够看到一些代码示例,将会很有帮助,因为我还没有找到任何相关资料。
注意:我正在使用Hadoop 2.7.2
编辑:我已经尝试了这里解释的方法,但并没有成功。我的情况不同,因为我想从另一个作业中访问计数器。(不是从mapper到reducer)。
我尝试做的事情:
public static void startFirstJob(String inputPath, String outputPath) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "wordCount");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCountMapper.class);
job.setCombinerClass(WordCountReducer.class);
job.setReducerClass(WordCountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(inputPath));
FileOutputFormat.setOutputPath(job, new Path(outputPath));
job.waitForCompletion(true);
}
在另一个类中定义了计数器枚举:
public class CountersClass {
public static enum N_COUNTERS {
SOMECOUNT
}
}
尝试读取计数器:
Cluster cluster = new Cluster(context.getConfiguration());
Job job = cluster.getJob(JobID.forName("wordCount"));
Counters counters = job.getCounters();
CountersClass.N_COUNTERS mycounter = CountersClass.N_COUNTERS.valueOf("SOMECOUNT");
Counter c1 = counters.findCounter(mycounter);
long N_Count = c1.getValue();