调试MR job, 最好在单机环境中,这样可以降低问题的复杂度。

一 推荐在eclipse下进行调试,所以先安装hadoop-eclipse 插件,注意对插件的jar进行修改

1: 向 lib 文件夹加入 依赖的jar包。

2 : 修改 meta-inf 文件

二 在eclipse中新建MR project,编写适当的逻辑,右键以run on hadoop 启动WordCount 的类,在此之前需要在工程的classpath下的hadoop配置文件中增加

 

   
   
   
   
  1. <property> 
  2.  
  3.          <name>mapred.child.java.optsname> 
  4.  
  5.          <value>-Xmx2000m -Xdebug -Xrunjdwp:transport=dt_socket,address=7788,server=y,suspend=yvalue> 
  6.  
  7.  property> 
  8.  
  9.  
  10.  
  11.  <property> 
  12.  
  13.          <name>mapred.tasktracker.map.tasks.maximumname> 
  14.  
  15.          <value>1value> 
  16.  
  17.          <description>tasktracker的map任务上限description> 
  18.  
  19.  property> 
  20.  
  21.  
  22.  
  23.  <property> 
  24.  
  25.          <name>mapred.tasktracker.reduce.tasks.maximumname> 
  26.  
  27.          <value>1value> 
  28.  
  29.          <description>tasktracker的reduce任务上限description> 
  30.  
  31.  property> 
  32.  
  33.  
  34.  
  35.  <property> 
  36.  
  37.          <name>mapred.task.timeoutname> 
  38.  
  39.          <value>100000000value> 
  40.  
  41.  property> 

 

这些配置指定了tasktracker启动jvm运行task时的参数。

三 因为我们的MR工程没有被打包,所以要有一个自打包程序(此段程序转载于网上

   
   
   
   
  1. import java.io.File; 
  2.  
  3. import java.io.FileInputStream; 
  4.  
  5. import java.io.FileOutputStream; 
  6.  
  7. import java.io.IOException; 
  8.  
  9. import java.net.MalformedURLException; 
  10.  
  11. import java.net.URL; 
  12.  
  13. import java.net.URLClassLoader; 
  14.  
  15. import java.util.LinkedList; 
  16.  
  17. import java.util.List; 
  18.  
  19. import java.util.jar.JarEntry; 
  20.  
  21. import java.util.jar.JarOutputStream; 
  22.  
  23. import java.util.jar.Manifest; 
  24.  
  25.  
  26.  
  27. public class EJob { 
  28.  
  29.      
  30.  
  31.     private static List classPath = new LinkedList(); 
  32.  
  33.      
  34.  
  35.     public static void addClasspath(String path){ 
  36.  
  37.         try { 
  38.  
  39.             classPath.add(new URL(path)); 
  40.  
  41.         } catch (MalformedURLException e) { 
  42.  
  43.             // TODO Auto-generated catch block 
  44.  
  45.             e.printStackTrace(); 
  46.  
  47.         } 
  48.  
  49.     } 
  50.  
  51.      
  52.  
  53.     public static ClassLoader getClassLoader() { 
  54.  
  55.         ClassLoader parent = Thread.currentThread().getContextClassLoader(); 
  56.  
  57.         if (parent == null) { 
  58.  
  59.             parent = EJob.class.getClassLoader(); 
  60.  
  61.         } 
  62.  
  63.         if (parent == null) { 
  64.  
  65.             parent = ClassLoader.getSystemClassLoader(); 
  66.  
  67.         } 
  68.  
  69.         return new URLClassLoader(classPath.toArray(new URL[0]), parent); 
  70.  
  71.     } 
  72.  
  73.      
  74.  
  75.     public static File createTempJar(String root) throws IOException { 
  76.  
  77.         if (!new File(root).exists()) { 
  78.  
  79.             return null
  80.  
  81.         } 
  82.  
  83.         Manifest manifest = new Manifest(); 
  84.  
  85.         manifest.getMainAttributes().putValue("Manifest-Version""1.0"); 
  86.  
  87.         final File jarFile = File.createTempFile("EJob-"".jar"new File(System 
  88.  
  89.                 .getProperty("java.io.tmpdir"))); 
  90.  
  91.  
  92.  
  93.         Runtime.getRuntime().addShutdownHook(new Thread() { 
  94.  
  95.             public void run() { 
  96.  
  97.                 jarFile.delete(); 
  98.  
  99.             } 
  100.  
  101.         }); 
  102.  
  103.  
  104.  
  105.         JarOutputStream out = new JarOutputStream(new FileOutputStream(jarFile), 
  106.  
  107.                 manifest); 
  108.  
  109.         createTempJarInner(out, new File(root), ""); 
  110.  
  111.         out.flush(); 
  112.  
  113.         out.close(); 
  114.  
  115.         return jarFile; 
  116.  
  117.     } 
  118.  
  119.  
  120.  
  121.     private static void createTempJarInner(JarOutputStream out, File f, 
  122.  
  123.             String base) throws IOException { 
  124.  
  125.         if (f.isDirectory()) { 
  126.  
  127.             File[] fl = f.listFiles(); 
  128.  
  129.             if (base.length() > 0) { 
  130.  
  131.                 base = base + "/"
  132.  
  133.             } 
  134.  
  135.             for (int i = 0; i < fl.length; i++) { 
  136.  
  137.                 createTempJarInner(out, fl[i], base + fl[i].getName()); 
  138.  
  139.             } 
  140.  
  141.         } else { 
  142.  
  143.             out.putNextEntry(new JarEntry(base)); 
  144.  
  145.             FileInputStream in = new FileInputStream(f); 
  146.  
  147.             byte[] buffer = new byte[1024]; 
  148.  
  149.             int n = in.read(buffer); 
  150.  
  151.             while (n != -1) { 
  152.  
  153.                 out.write(buffer, 0, n); 
  154.  
  155.                 n = in.read(buffer); 
  156.  
  157.             } 
  158.  
  159.             in.close(); 
  160.  
  161.         } 
  162.  
  163.     } 
  164.  
  165.  
  166.  
最后在WordCount的main函数中增加
 
    
    
    
    
  1. File jarFile = EJob.createTempJar("bin"); 
  2. ClassLoader classLoader = EJob.getClassLoader(); 
  3. Thread.currentThread().setContextClassLoader(classLoader); 
  4.  
  5. ((JobConf)job.getConfiguration()).setJar(jarFile.toString()); 
 
这样在eclipse建立远程调试就可以连接上7788端口了