地图上的条件减少计数器以控制地图输出

时间:2015-10-08 10:28:30

标签: java hadoop mapreduce counter

是否有机会在映射器级别对用户定义的java计数器设置条件控制映射器输出?

       Long l = context.getCounter(Counters.COUNT).getValue();

        if(5L >= l) {
            context.getCounter(Counters.COUNT).increment(1);
            context.write((LongWritable)key, value);
        } else {
            System.out.println("MAP ELSE");
            return;
        }

获得超过五个记录输入到reducer。 有没有机会控制这个。???

1 个答案:

答案 0 :(得分:1)

你不能这样做,如果你的输入文件有3个分割,那么你将有3个映射器在运行。每个映射器都有其单独的计数值(取决于逻辑如何递增计数值),并且只有在所有映射器在混洗阶段之后完成时才会在reduce侧知道。

如果要限制地图输出。然后有一个reducer job.setNumReduceTasks(1)并限制reducer的输出。这样的事情。

public static class WLReducer2 extends
        Reducer<IntWritable, Text, Text, IntWritable> {
    int count=0;
    @Override
    protected void reduce(IntWritable key, Iterable<Text> values,
            Context context) throws IOException, InterruptedException {

        for (Text x : values) {
            if (count < 5)
            context.write(key, x);
            count++;
        }

    };
}

如果您想在减少方面获得计数器值。您可以将其添加到reduce设置方法。

 @Override
    public void setup(Context context) throws IOException, InterruptedException{
        Configuration conf = context.getConfiguration();
        Cluster cluster = new Cluster(conf);
        Job currentJob = cluster.getJob(context.getJobID());
        mapperCounter = currentJob.getCounters().findCounter(COUNTER_NAME).getValue();  
    }
相关问题