You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by cu...@apache.org on 2006/02/09 19:27:59 UTC

svn commit: r376375 - in /lucene/hadoop/trunk/src: examples/org/apache/hadoop/examples/Grep.java examples/org/apache/hadoop/examples/WordCount.java java/org/apache/hadoop/mapred/JobConf.java

Author: cutting
Date: Thu Feb  9 10:27:57 2006
New Revision: 376375

URL: http://svn.apache.org/viewcvs?rev=376375&view=rev
Log:
Add an easy way to specify a job's jar, by naming a class in the jar.

Modified:
    lucene/hadoop/trunk/src/examples/org/apache/hadoop/examples/Grep.java
    lucene/hadoop/trunk/src/examples/org/apache/hadoop/examples/WordCount.java
    lucene/hadoop/trunk/src/java/org/apache/hadoop/mapred/JobConf.java

Modified: lucene/hadoop/trunk/src/examples/org/apache/hadoop/examples/Grep.java
URL: http://svn.apache.org/viewcvs/lucene/hadoop/trunk/src/examples/org/apache/hadoop/examples/Grep.java?rev=376375&r1=376374&r2=376375&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/examples/org/apache/hadoop/examples/Grep.java (original)
+++ lucene/hadoop/trunk/src/examples/org/apache/hadoop/examples/Grep.java Thu Feb  9 10:27:57 2006
@@ -51,7 +51,7 @@
       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 @@
 
     JobClient.runJob(grepJob);
 
-    JobConf sortJob = new JobConf(defaults);
+    JobConf sortJob = new JobConf(defaults, Grep.class);
 
     sortJob.setInputDir(tempDir);
     sortJob.setInputFormat(SequenceFileInputFormat.class);

Modified: lucene/hadoop/trunk/src/examples/org/apache/hadoop/examples/WordCount.java
URL: http://svn.apache.org/viewcvs/lucene/hadoop/trunk/src/examples/org/apache/hadoop/examples/WordCount.java?rev=376375&r1=376374&r2=376375&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/examples/org/apache/hadoop/examples/WordCount.java (original)
+++ lucene/hadoop/trunk/src/examples/org/apache/hadoop/examples/WordCount.java Thu Feb  9 10:27:57 2006
@@ -110,12 +110,8 @@
   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)

Modified: lucene/hadoop/trunk/src/java/org/apache/hadoop/mapred/JobConf.java
URL: http://svn.apache.org/viewcvs/lucene/hadoop/trunk/src/java/org/apache/hadoop/mapred/JobConf.java?rev=376375&r1=376374&r2=376375&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/java/org/apache/hadoop/mapred/JobConf.java (original)
+++ lucene/hadoop/trunk/src/java/org/apache/hadoop/mapred/JobConf.java Thu Feb  9 10:27:57 2006
@@ -23,6 +23,9 @@
 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 @@
   }
 
 
+  /** 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
@@ -270,6 +284,31 @@
     if (result instanceof JobConfigurable)
       ((JobConfigurable)result).configure(this);
     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;
   }
 
 }