【问题标题】:how to access Mapper Counter value in a Reducer?如何在 Reducer 中访问 Mapper Counter 值?
【发布时间】:2020-03-26 11:13:15
【问题描述】:

我想访问 reducer 中的 myCounter.my 值:

public static class Map extends Mapper<LongWritable, Text, ImmutableBytesWritable, ImmutableBytesWritable>
{
    public static enum myCounter{my};

    @Override
    public void map(LongWritable key, Text value, Context context) 
    {
        context.getCounter(myCounter.my).increment(1);
        context.write( new ImmutableBytesWritable ( ),new ImmutableBytesWritable() );
    }
}


public static class Reduce extends Reducer<ImmutableBytesWritable, ImmutableBytesWritable, Text, Text>
{
    @Override
    public void reduce(ImmutableBytesWritable key,Iterable<ImmutableBytesWritable> result,Context context)
    {

    }
}

Accessing a mapper's counter from a reducer(for old API is given ) 如何使它适用于新的 API?

或者

我想知道映射器输出的总数?有没有更好的办法? (我无法访问 Reducer 中的计数器:

Group Name-&gt;org.apache.hadoop.mapred.Task$Counter Counter Name-&gt;MAP_OUTPUT_RECORDS)

谢谢

【问题讨论】:

  • 您使用的是哪个 hadoop 版本?
  • @faizan: ty 但正如您所看到的,示例是通过 Job 访问计数器,只有在 Job 完成破坏后才能在 main(String args[]) 或其他函数中,我想在 Reduce 中访问它的位置。所以我的问题仍然没有解决。

标签: java hadoop mapreduce


【解决方案1】:

您可以通过作业对象访问计数器,使其适用于新 API。

Configuration conf = context.getConfiguration();
Cluster cluster = new Cluster(conf);
Job currentJob = cluster.getJob(context.getJobID());
long val=currentJob.getCounters().findCounter(myCounter.my).getValue();

【讨论】:

    【解决方案2】:

    您可以使用相同的代码在 reducer 中访问您的计数器值

    Counter counter = context.getCounter( myCounter.my );
            counter.getValue();
    

    also see

    【讨论】:

    • It's Not working .counter.getValue 将给出零,因为 Reducer 中的上下文(org.apache.hadoop.mapreduce.Reducer.Context )与上下文不同(org.apache.hadoop.mapreduce.Reducer.Context ) Mapper .
    • 据我所知,上下文使您可以访问相同的全局计数器。
    • 所以它不会影响您是从 setup 还是 map 或 reduce 方法访问上下文 link
    • 起初,我试过了,但它不起作用,它返回零,所以我得出了这个结论。
    • 我想你会得到你的答案here
    【解决方案3】:

    您可以通过以下方式访问:

    Map.myCounter.my // static fields you can access by it's class name.
    

    【讨论】:

    • 在 hadoop Map-Reduce 框架中它不能像这样工作。我的问题是针对 Map_reduce 框架的。
    猜你喜欢
    • 2018-08-02
    • 2014-04-03
    • 1970-01-01
    • 2016-06-21
    • 2013-11-05
    • 1970-01-01
    • 2017-06-29
    • 2016-09-16
    • 1970-01-01
    相关资源
    最近更新 更多