Browse Source

HADOOP-1767. Add 'bin/hadoop job -list' sub-command. Contributed by Christophe Taton.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@570879 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 18 years ago
parent
commit
208b02fd1d
2 changed files with 74 additions and 28 deletions
  1. 2 0
      CHANGES.txt
  2. 72 28
      src/java/org/apache/hadoop/mapred/JobClient.java

+ 2 - 0
CHANGES.txt

@@ -31,6 +31,8 @@ Trunk (unreleased changes)
     HADOOP-1708.  Make files appear in namespace as soon as they are
     created.  (Dhruba Borthakur via dhruba)
 
+    HADOOP-1767.  Add "hadoop job -list" command. (taton via cutting)
+
   OPTIMIZATIONS
 
     HADOOP-1565.  Reduce memory usage of NameNode by replacing 

+ 72 - 28
src/java/org/apache/hadoop/mapred/JobClient.java

@@ -757,39 +757,59 @@ public class JobClient extends Configured implements MRConstants, Tool  {
   public TaskStatusFilter getTaskOutputFilter(){
     return this.taskOutputFilter; 
   }
+
+  /**
+   * Display usage of the command-line tool and terminate execution
+   */
+  private void displayUsage() {
+    System.out.printf("JobClient <command> <args>\n");
+    System.out.printf("\t-submit\t<job-file>\n");
+    System.out.printf("\t-status\t<job-id>\n");
+    System.out.printf("\t-kill\t<job-id>\n");
+    System.out.printf("\t-events\t<job-id> <from-event-#> <#-of-events>\n");
+    System.out.printf("\t-list\n\n");
+    throw new RuntimeException("JobClient: bad command-line arguments");
+  }
     
   public int run(String[] argv) throws Exception {
-    if (argv.length < 2) {
-      String cmd = "JobClient -submit <job> | -status <id> |" + 
-                   " -events <id> |" +
-                   " -kill <id> [-jt <jobtracker:port>|<config>]";
-      System.out.println(cmd);
-      throw new RuntimeException("JobClient:" + cmd);
-    }
-
-    // Process args
+    // process arguments
     String submitJobFile = null;
     String jobid = null;
+    int fromEvent = 0;
+    int nEvents = 0;
     boolean getStatus = false;
     boolean killJob = false;
-
-    for (int i = 0; i < argv.length; i++) {
-      if ("-submit".equals(argv[i])) {
-        submitJobFile = argv[i+1];
-        i++;
-      } else if ("-status".equals(argv[i])) {
-        jobid = argv[i+1];
-        getStatus = true;
-        i++;
-      } else if ("-kill".equals(argv[i])) {
-        jobid = argv[i+1];
-        killJob = true;
-        i++;
-      } else if ("-events".equals(argv[i])) {
-        listEvents(argv[i+1], Integer.parseInt(argv[i+2]), 
-                   Integer.parseInt(argv[i+3]));
-        i += 3;
-      }
+    boolean listEvents = false;
+    boolean listJobs = false;
+
+    if (argv.length < 1)
+      displayUsage();
+
+    if ("-submit".equals(argv[0])) {
+      if (argv.length != 2)
+        displayUsage();
+      submitJobFile = argv[1];
+    } else if ("-status".equals(argv[0])) {
+      if (argv.length != 2)
+        displayUsage();
+      jobid = argv[1];
+      getStatus = true;
+    } else if ("-kill".equals(argv[0])) {
+      if (argv.length != 2)
+        displayUsage();
+      jobid = argv[1];
+      killJob = true;
+    } else if ("-events".equals(argv[0])) {
+      if (argv.length != 4)
+        displayUsage();
+      jobid = argv[1];
+      fromEvent = Integer.parseInt(argv[2]);
+      nEvents = Integer.parseInt(argv[3]);
+      listEvents = true;
+    } else if ("-list".equals(argv[0])) {
+      listJobs = true;
+    } else {
+      displayUsage();
     }
 
     // initialize JobClient
@@ -807,6 +827,7 @@ public class JobClient extends Configured implements MRConstants, Tool  {
       if (submitJobFile != null) {
         RunningJob job = submitJob(conf);
         System.out.println("Created job " + job.getJobID());
+        exitCode = 0;
       } else if (getStatus) {
         RunningJob job = getJob(jobid);
         if (job == null) {
@@ -825,13 +846,19 @@ public class JobClient extends Configured implements MRConstants, Tool  {
           System.out.println("Killed job " + jobid);
           exitCode = 0;
         }
+      } else if (listEvents) {
+        listEvents(jobid, fromEvent, nEvents);
+        exitCode = 0;
+      } else if (listJobs) {
+        listJobs();
+        exitCode = 0;
       }
     } finally {
       close();
     }
     return exitCode;
   }
-    
+
   /**
    * List the events for the given job
    * @param jobId the job id for the job's events to list
@@ -849,6 +876,23 @@ public class JobClient extends Configured implements MRConstants, Tool  {
                          " " + event.getTaskTrackerHttp());
     }
   }
+
+  /**
+   * Dump a list of currently running jobs
+   * @throws IOException
+   */
+  private void listJobs() throws IOException {
+    JobStatus[] jobs = jobsToComplete();
+    if (jobs == null)
+      jobs = new JobStatus[0];
+
+    System.out.printf("%d jobs currently running\n", jobs.length);
+    System.out.printf("JobId\tState\tStartTime\tUserName\n");
+    for (JobStatus job : jobs) {
+      System.out.printf("%s\t%d\t%d\t%s\n", job.getJobId(), job.getRunState(),
+          job.getStartTime(), job.getUsername());
+    }
+  }
     
   /**
    */