Przeglądaj źródła

Add an easy way to specify a job's jar, by naming a class in the jar.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@376375 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 19 lat temu
rodzic
commit
100acbfd9c

+ 2 - 2
src/examples/org/apache/hadoop/examples/Grep.java

@@ -51,7 +51,7 @@ public class Grep {
       new File("grep-temp-"+
                Integer.toString(new Random().nextInt(Integer.MAX_VALUE)));
 
-    JobConf grepJob = new JobConf(defaults);
+    JobConf grepJob = new JobConf(defaults, Grep.class);
 
     grepJob.setInputDir(new File(args[0]));
 
@@ -70,7 +70,7 @@ public class Grep {
 
     JobClient.runJob(grepJob);
 
-    JobConf sortJob = new JobConf(defaults);
+    JobConf sortJob = new JobConf(defaults, Grep.class);
 
     sortJob.setInputDir(tempDir);
     sortJob.setInputFormat(SequenceFileInputFormat.class);

+ 2 - 6
src/examples/org/apache/hadoop/examples/WordCount.java

@@ -110,12 +110,8 @@ public class WordCount {
   public static void main(String[] args) throws IOException {
     Configuration defaults = new Configuration();
     
-    JobConf countJob = new JobConf(defaults);
-    
-    URL jar_url = WordCount.class.getClassLoader().
-    getResource("hadoop-examples.jar");
-    countJob.setJar(jar_url.getPath());
-    
+    JobConf countJob = new JobConf(defaults, WordCount.class);
+ 
     // the keys are words (strings)
     countJob.setOutputKeyClass(UTF8.class);
     // the values are counts (ints)

+ 39 - 0
src/java/org/apache/hadoop/mapred/JobConf.java

@@ -23,6 +23,9 @@ import java.io.File;
 import java.util.StringTokenizer;
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Enumeration;
+
+import java.net.URL;
 
 import org.apache.hadoop.fs.FileUtil;
 import org.apache.hadoop.conf.Configuration;
@@ -59,6 +62,17 @@ public class JobConf extends Configuration {
   }
 
 
+  /** Construct a map/reduce job configuration.
+   * 
+   * @param conf a Configuration whose settings will be inherited.
+   * @param aClass a class whose containing jar is used as the job's jar.
+   */
+  public JobConf(Configuration conf, Class aClass) {
+    this(conf);
+    setJar(findContainingJar(aClass));
+  }
+
+
   /** Construct a map/reduce configuration.
    *
    * @param config a Configuration-format XML job description file
@@ -272,5 +286,30 @@ public class JobConf extends Configuration {
     return result;
   }
 
+  /** Find a jar that contains a class of the same name, if any.
+   * It will return a jar file, even if that is not the first thing
+   * on the class path that has a class with the same name.
+   * @author Owen O'Malley
+   * @param my_class the class to find
+   * @return a jar file that contains the class, or null
+   * @throws IOException
+   */
+  private static String findContainingJar(Class my_class) {
+    ClassLoader loader = my_class.getClassLoader();
+    String class_file = my_class.getName().replaceAll("\\.", "/") + ".class";
+    try {
+      for(Enumeration itr = loader.getResources(class_file);
+          itr.hasMoreElements();) {
+        URL url = (URL) itr.nextElement();
+        if ("jar".equals(url.getProtocol())) {
+          return url.getPath().replace("file:", "").replaceAll("!.*$", "");
+        }
+      }
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+    return null;
+  }
+
 }