hadoop命令行中自定义计数器

来源:互联网 发布:mac怎么连接校园网 编辑:程序博客网 时间:2024/05/21 06:15
/**
  * 缩进较短的称为计数器组
  * 缩进较长的称为计数器组下的计数器
*/
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));}};}


执行效果如下:





0 0