MapReduce-计数器

MapReduce 中的计数器

计数器是收集作业统计信息的有效手段之一,用于质量控制或应用级统计。计数器还可辅助诊断系统故障。如果需要将日志信息传输到 map 或 reduce 任务, 更好的方法通常是看能否用一个计数器值来记录某一特定事件的发生。对于大型分布式作业而言,使用计数器更为方便。除了因为获取计数器值比输出日志更方便,还有根据计数器值统计特定事件的发生次数要比分析一堆日志文件容易得多。

hadoop内置计数器列表

MapReduce任务
计数器
org.apache.hadoop.mapreduce.TaskCounter
文件系统计数器org.apache.hadoop.mapreduce.FileSystemCounter
FileInputFormat
计数器
org.apache.hadoop.mapreduce.lib.input.FileInputFormatCounter
FileOutputFormat
计数器
org.apache.hadoop.mapreduce.lib.output.FileOutputFormatCounter
作业计数器org.apache.hadoop.mapreduce.JobCounter

所有的这些都是MapReduce的计数器的功能,既然MapReduce当中有计数器的功能,我们如何实现自己的计数器???

需求:以上面排序以及序列化为案例,统计map接收到的数据记录条数

第一种方式

第一种方式定义计数器,通过context上下文对象可以获取我们的计数器,进行记录通过context上下文对象,在map端使用计数器进行统计

public class SortMapper extends
Mapper<LongWritable,Text,PairWritable,IntWritable> {

	private PairWritable mapOutKey = new PairWritable();
	
	private IntWritable mapOutValue = new IntWritable();
	
	@Override
	public void map(LongWritable key, Text value, Context context) throws
		IOException, InterruptedException {
			//自定义我们的计数器,这里实现了统计map数据数据的条数
			Counter counter = context.getCounter("MR_COUNT",
			"MapRecordCounter");
			counter.increment(1L);
			String lineValue = value.toString();
			String[] strs = lineValue.split("\t");
			//设置组合key和value ==> <(key,value),value>
			mapOutKey.set(strs[0], Integer.valueOf(strs[1]));
			mapOutValue.set(Integer.valueOf(strs[1]));
			context.write(mapOutKey, mapOutValue);
	}
	
}

运行程序之后就可以看到我们自定义的计数器在map阶段读取了七条数据

第二种方式

通过enum枚举类型来定义计数器

统计reduce端数据的输入的key有多少个,对应的value有多少个

public class SortReducer extends
	Reducer<PairWritable,IntWritable,Text,IntWritable> {
	
	private Text outPutKey = new Text();
	
	public static enum Counter{
		REDUCE_INPUT_RECORDS, REDUCE_INPUT_VAL_NUMS,
	}
	
	@Override
	public void reduce(PairWritable key, Iterable<IntWritable> values,
		Context context) throws IOException, InterruptedException {		
			context.getCounter(Counter.REDUCE_INPUT_RECORDS).increment(1L);
			//迭代输出
			for(IntWritable value : values) {
			context.getCounter(Counter.REDUCE_INPUT_VAL_NUMS).increment(1L);
			outPutKey.set(key.getFirst());
			context.write(outPutKey, value);		
		}
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值