hadoop命令行中自定义计数器

本文详细介绍了如何在MapReduce任务中利用自定义计数器追踪特定操作,如敏感词识别和数据读写情况。通过示例代码展示了如何实现并解释了关键指标,包括输入输出记录数、内存使用、数据读写等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
  * 缩进较短的称为计数器组
  * 缩进较长的称为计数器组下的计数器
*/
hello you
hello me



Counters: 19
  File Output Format Counters 
    Bytes Written=19 //写出去多少字节
  FileSystemCounters //包含HDFS系统文件  (所以略)
    FILE_BYTES_READ=333
    HDFS_BYTES_READ=38
    FILE_BYTES_WRITTEN=129076
    HDFS_BYTES_WRITTEN=19
  File Input Format Counters //包含HDFS系统文件  (所以略)
    Bytes Read=19//从输入文件读取的字节数
------------------重点---------------------------
  Map-Reduce Framework
    Map output materialized bytes=65
    Map input records=2//读取记录的行数  hello you   hello me两行
    Reduce shuffle bytes=0
    Spilled Records=8
    Map output bytes=51
    Total committed heap usage (bytes)=385875968
    SPLIT_RAW_BYTES=86
    Combine input records=4 //规约输入
    Reduce input records=4 //reduce输入的记录数  (等于map的输出)
    Reduce input groups=3  //reduce输入的组数      (<hello,{1,1}><me,1><you,1>共3组)
    Combine output records=3 //规约输出
    Reduce output records=3 //reduce输出的记录数 (<hello,2><me,1><you,1>共3组)

    Map output records=4   //输入的记录行   hello 1  you 1  hello 1 me 1




自定义计数器:

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Mapper;

public class MyMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
	protected void map(LongWritable key, Text value,
			org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, Text, LongWritable>.Context context)
			throws java.io.IOException, InterruptedException {
		/**
		 * 自定义计数器
		 *     当key值出现hello时,就加1
		 *     第一个参数表示计数器组名,也就是缩进较短的那种
		 *     第二个参数是计数器组下的计数器的名字,也就是缩进较长的那种
		 */
		Counter counter = context.getCounter("敏感词", "hello");  
		String val = value.toString();
		if(val.contains("hello")){
			//记录敏感词出现的次数
			counter.increment(1l);
		}
		String[] values = val.split("\t");
		for (String string : values) {
			context.write(new Text(string), new LongWritable(1));
		}
	};
}


执行效果如下:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值