如何通过API访问Hadoop计数器值?

在Hadoop中我们可以在map / reduce任务中增加计数器,它看起来像这样:

... context.getCounter(MyCountersEnum.SomeCounter).increment(1); ... 

你可以在日志中找到它们的价值。

工作完成后如何从代码中访问它们?

什么是Hadoop API来读取计数器值?

计数器代表全局计数器 ,由Map-Reduce框架或应用程序定义。

每个计数器可以是任何枚举类型。 您可以将计数器定义为Driver类中的枚举

 static enum UpdateCount{ CNT } 

然后在map / reduce任务中递增计数器

 public class CntReducer extends Reducer{ public void reduce(IntWritable key,Iterable values,Context context) { //do something context.getCounter(UpdateCount.CNT).increment(1); } } 

并在Driver类中访问它们

 public int run(String[] args) throws Exception { . . . job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.setInputPaths(job,in ); FileOutputFormat.setOutputPath(job, out); job.waitForCompletion(true); c = job.getCounters().findCounter(UpdateCount.CNT).getValue(); //Print "c" } } 

c给出计数器值。

你可以在这里找到一个例子

我刚刚在这里找到答案。

您需要一个作业对象来访问计数器:

 Counters counters = job.getCounters(); Counter counter = counters.findCounter(MyCountersEnum.SomeCounter); System.out.println(counter.getDisplayName() + ": " + counter.getValue()); 
    Interesting Posts