混淆 hadoop 作业跟踪器 api

2022-09-02 10:38:35

我试图从工作跟踪器中收集一些信息。对于初学者,我想从获取正在运行的作业信息(例如作业ID或作业名称等)开始。但是已经卡住了,这是我得到的(打印出当前正在运行的作业的作业ID):

public static void main(String[] args) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "zk1.myhost,zk2.myhost,zk3.myhost");
        conf.set("hbase.zookeeper.property.clientPort", "2181");

        InetSocketAddress jobtracker = new InetSocketAddress("jobtracker.mapredhost.myhost", 8021);
        JobClient jobClient = new JobClient(jobtracker, conf);
        JobStatus[] jobs = jobClient.jobsToComplete();

        for (int i = 0; i < jobs.length; i++) {
            JobStatus js = jobs[i];
            if (js.getRunState() == JobStatus.RUNNING) {
                JobID jobId = js.getJobID();
                System.out.println(jobId);
            }
        }
    }

在尝试显示作业 ID 时,上面的内容可以作为魅力,但现在我也想显示作业名称。所以我在打印作业ID后添加了这行:

System.out.println(jobClient.getJob(jobId).getJobName());

我得到这个例外:

Exception in thread "main" java.lang.NullPointerException
    at org.apache.hadoop.mapred.JobClient$NetworkedJob.<init>(JobClient.java:226)
    at org.apache.hadoop.mapred.JobClient.getJob(JobClient.java:1080)
    at org.apache.test.JobTracker.main(JobTracker.java:28)

jobClient莫。我知道这一点,因为我尝试使用空检查if语句,但这是.我在这里做错了什么?nulljobClient.getJob(jobId)null

根据API,我应该没问题,

http://hadoop.apache.org/mapreduce/docs/r0.21.0/api/org/apache/hadoop/mapred/JobClient.html#getJob(org.apache.hadoop.mapred.JobID)

首先从 jobClient 获取,而不是在运行作业后获取它的名称 http://hadoop.apache.org/mapreduce/docs/r0.21.0/api/org/apache/hadoop/mapred/RunningJob.html#getJobName()RunningJob

以前有人做过这样的事情吗?我可以使用jsoup通过GET请求获取此信息,但我认为这是获取此信息的更好方法。

问题更新这里是我的hadoop / hbase依赖关系:

<dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>0.23.1-mr1-cdh4.0.0b2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-core</artifactId>
            <version>0.23.1-mr1-cdh4.0.0b2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mortbay.jetty</groupId>
                    <artifactId>jetty</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase</artifactId>
            <version>0.92.1-cdh4b2-SNAPSHOT</version>
        </dependency>

赏金更新 :

这是我的进口:

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobID;
import org.apache.hadoop.mapred.JobStatus;

下面是的输出:System.out.println(jobId)

job_201207031810_1603

当前只有一个作业正在运行。


答案 1

看看 的内部类。
(来源:/home/user/hadoop/src/mapred/org/apache/hadoop/mapred/JobClient.java)NetworkedJobJobClient

它的构造函数尝试从第 225 行中获取对象,但它为 null,因为它没有设置它:ConfigurationJobClientnew JobClient(InetSocketAddress jobTrackAddr, Configuration conf)

// Set the completion poll interval from the configuration.
      // Default is 5 seconds.
      Configuration conf = JobClient.this.getConf();
      this.completionPollIntervalMillis = conf.getInt(COMPLETION_POLL_INTERVAL_KEY,
          DEFAULT_COMPLETION_POLL_INTERVAL); //NPE occurs here!

解决方法是在创建 JobClient 对象后手动设置它。这将解决您的问题:

..
JobClient jobClient = new JobClient(jobtracker, conf);
jobClient.setConf(conf); 
....

附注:

我通过以下方式实例化了对象:Configuration

Configuration conf = new Configuration();
conf.addResource(new Path("/path_to/core-site.xml"));
conf.addResource(new Path("/path_to/hdfs-site.xml"));

答案 2

推荐