MapReduce 计数器

MapReduce计数器(Counter)是用来记录job的执行进度和状态的。它的作用可以理解为日志。我们通常可以在程序的某个位置插入计数器,用来记录数据或者进度的变化情况,它比日志更便利进行分析。
计数器为我们提供一个窗口,用于观察 MapReduce Job 运行期的各种细节数据。对MapReduce性能调优很有帮助,MapReduce性能优化的评估大部分都是基于这些 Counter 的数值表现出来的。
MapReduce 自带了许多默认Counter,方便大家观察 Job 结果,如输入的字节数、输出的字节数、Map端输入/输出的字节数和条数、Reduce端的输入/输出的字节数和条数等。我们只需了解这些内置计数器,知道计数器组名称(groupName)和计数器名称(counterName),以后使用计数器查找groupName和counterName即可。
1.MapReduce任务计数器,groupName为org.apache.hadoop.mapreduce.TaskCounter
2.文件系统计数器,groupName为org.apache.hadoop.mapreduce.FileSystemCounter
3.FileInputFormat计数器,groupName为org.apache.hadoop.mapreduce.lib.input.FileInputFormatCounter
4.FileOutputFormat计数器,groupName为org.apache.hadoop.mapreduce.lib.input.FileOutputFormatCounter
5.作业计数器由 JobTracker(或者 YARN)维护,因此无需在网络间传输数据,这一点与包括自定义计数器在内的其它计数器不同。这些计数器都是作业级别的统计量,其值不会随着任务运行而改变。 作业计数器计数器的 groupNam
为org.apache.hadoop.mapreduce.JobCounter
6.自定义计数器
可以使用Context的getCounter()方法(其实是接口TaskAttemptContext的方法,Context继承了该接口)得到自定义计数器。

可以通过枚举或者字符串来得到计数器。
public Counter getCounter(Enum< > counterName):Get the Counter for the given counterName
public Counter getCounter(String groupName, String counterName):Get the Counter for the given groupName and counterName
计数器常见的方法有几下几个:
String getName():Get the name of the counter
String getDisplayName():Get the display name of the counter
long getValue():Get the current value
void setValue(long value):Set this counter by the given value
void increment(long incr):Increment this counter by the given value

计数器的使用
// 自定义枚举变量Enum

Counter counter = context.getCounter(Enum enum)
// 自己命名groupName和counterName 
Counter counter = context.getCounter(String groupName,String counterName)
counter.setValue(long value);// 设置初始值
counter.increment(long incr);// 增加计数

获取枚举计数器的值

Configuration conf = new Configuration(); 
Job job = new Job(conf, "MyCounter"); 
job.waitForCompletion(true); 
Counters counters=job.getCounters(); 
Counter counter=counters.findCounter(LOG_PROCESSOR_COUNTER.
BAD_RECORDS_LONG);// 查找枚举计数器,假如Enum的变量为BAD_RECORDS_LONG 
long value=counter.getValue();//获取计数值

获取自定义计数器的值

Configuration conf = new Configuration(); 
Job job = new Job(conf, "MyCounter"); 
job.waitForCompletion(true); 
Counters counters = job.getCounters(); 
Counter counter=counters.findCounter("ErrorCounter","toolong");// 假如groupName为ErrorCounter,counterName为toolong 
long value = counter.getValue();// 获取计数值

获取内置计数器的值

Configuration conf = new Configuration(); 
Job job = new Job(conf, "MyCounter"); 
job.waitForCompletion(true); 
Counters counters=job.getCounters(); 
// 查找作业运行启动的reduce个数的计数器,groupName和counterName可以从内置计数器表格查询
Counter counter=counters.findCounter("org.apache.hadoop.mapreduce.
JobCounter","TOTAL_LAUNCHED_REDUCES");// 假如groupName为org.apache.hadoop.mapreduce.JobCounter,counterName为TOTAL_LAUNCHED_REDUCES 
long value=counter.getValue();// 获取计数值

获取所有计数器的值

Configuration conf = new Configuration(); 
Job job = new Job(conf, "MyCounter"); 
Counters counters = job.getCounters(); 
for (CounterGroup group : counters) { 
  for (Counter counter : group) { 
    System.out.println(counter.getDisplayName() + ": " + counter.getName() + ": "+ counter.getValue()); 
  } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值