【问题标题】:How to access JobCounters and FileSystemCounters in Hadoop 2.6?如何在 Hadoop 2.6 中访问 JobCounters 和 FileSystemCounters?
【发布时间】:2015-04-04 16:51:49
【问题描述】:

在我的 MapReduce 程序的 Reducer 中,我希望读取 JobCounterFileSystemCounter。运行命令mapred job -status <job id> 时,我需要的计数器按显示名称列出:

...
File System Counters
    FILE: Number of bytes read=148874
    FILE: Number of bytes written=22010065
    FILE: Number of read operations=0
    FILE: Number of large read operations=0
    FILE: Number of write operations=0
    HDFS: Number of bytes read=135823
    HDFS: Number of bytes written=44423504133
    HDFS: Number of read operations=2185
    HDFS: Number of large read operations=0
    HDFS: Number of write operations=1316
Job Counters 
    Launched map tasks=1
    Launched reduce tasks=200
    Rack-local map tasks=1
    Total time spent by all maps in occupied slots (ms)=5293
    Total time spent by all reduces in occupied slots (ms)=972893
    Total time spent by all map tasks (ms)=5293
    Total time spent by all reduce tasks (ms)=972893
    Total vcore-seconds taken by all map tasks=5293
    Total vcore-seconds taken by all reduce tasks=972893
    Total megabyte-seconds taken by all map tasks=5420032
    Total megabyte-seconds taken by all reduce tasks=996242432
...

如何在运行时从Reducer 的代码中访问这些计数器?

使用 Google 时,我找不到任何有关如何访问这些计数器的有用信息。使用Context.getCounter(String groupName, String counterName) 的直接尝试未能检索到Counter 实例,因此在调用getValue() 时抛出NullPointerException

long bytes = context.getCounter(
    FileSystemCounter.class.getName(),
    FileSystemCounter.BYTES_WRITTEN.name()
).getValue();
long milliseconds = context.getCounter(
    JobCounter.class.getName(),
    JobCounter.MILLIS_REDUCES.name()
).getValue();

【问题讨论】:

    标签: java hadoop mapreduce counter


    【解决方案1】:
    Counters counters = job.getCounters();
    
    for (CounterGroup group : counters) {
          System.out.println("* Counter Group: " + group.getDisplayName() + " (" + group.getName() + ")");
          System.out.println("  number of counters in this group: " + group.size());
          for (Counter counter : group) {
            System.out.println("  - " + counter.getDisplayName() + ": " + counter.getName() + ": "+counter.getValue());
          }
        }
    

    我认为这将有助于打印所有计数器及其值。

    【讨论】:

    • 快到了;两个问题:第一,变量job从哪里来? Reducer.Context 没有返回Job 的方法。二、为什么Reducer.Context.getCounter(String groupName, String counterName)不起作用?
    • 配置 conf = new Configuration();集群集群 = 新集群(conf); Job job = Job.getInstance(cluster,conf);
    • 在运行时不起作用;我得到java.lang.Exception: java.lang.IllegalStateException: Job in state DEFINE instead of RUNNING。似乎Job.getInstance() 创建了一个新的Job,但我需要当前正在运行的那个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    相关资源
    最近更新 更多