计数器与自定义计数器

https://www.cnblogs.com/edisonchou/p/4297599.html

实例:

链接:https://pan.baidu.com/s/1qeryRGn2oyXeyLSqabKBBA 
提取码:5wia 
复制这段内容后打开百度网盘手机App,操作更方便哦

关键代码:

package com.example.demo.mapred;

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

public class WCMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //普通计数器
//        Text keyOut = new Text();
//        IntWritable valueOut = new IntWritable();
//        String[] arr = value.toString().split(" ");
//        for(String s : arr){
//            keyOut.set(s);
//            valueOut.set(1);
//            context.write(keyOut,valueOut);
//        }
//        *******************************************************
//        自定义计数器
//        这里有两个形参,第一个是计数器组的名称,第二是计数器的名称。
        Counter sensitiveCounter = context.getCounter("Sensitive Words:", "Hello");
        String line = value.toString();
        // 这里假定Hello是一个敏感词
        if(line.contains("Hello")){
            sensitiveCounter.increment(1L);
        }
        String[] spilted = line.split(" ");
        for (String word : spilted) {
            context.write(new Text(word), new IntWritable());
        }

    }


}

猜你喜欢

转载自blog.csdn.net/ssllkkyyaa/article/details/86492817
今日推荐