Hadoop学习之修改Eclipse插件

来源:互联网 发布:骑行软件下载 编辑:程序博客网 时间:2024/06/08 12:53

之前手工成功编译Hadoop-1.2.1的Eclipse插件后,发现了若干问题,比如生成的Mapper和Reducer还在使用Hadoop-0.x版本的一些类,为了解决这些问题并使插件能够适应Hadoop-1.2.1的变化,决定修改插件的源代码后再重新编译。

首先需要确定要修改哪些类,在仔细观察了hadoop-1.2.1/src/contrib/eclipse-plugin/src/java/org/apache/hadoop/eclipse目录下的源代码和使用Eclipse开发Hadoop程序时的向导,发现只需要修改NewMapperWizard.java、NewReducerWizard.java、NewDriverWizardPage.java这几个文件就可以使Eclipse插件适应Hadoop-1.2.1版本。在详细介绍如何修改源文件之前,先看看未修改时创建Hadoop应用程序的向导和生成的Mapper、Reducer是什么样子的。

在Eclipse中新建Mapper时的向导如下图所示:


生成的Mapper程序如下:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. import java.io.IOException;  
  2. import org.apache.hadoop.io.Writable;  
  3. import org.apache.hadoop.io.WritableComparable;  
  4. import org.apache.hadoop.mapred.MapReduceBase;  
  5. import org.apache.hadoop.mapred.Mapper;  
  6. import org.apache.hadoop.mapred.OutputCollector;  
  7. import org.apache.hadoop.mapred.Reporter;  
  8. public class Mapper1 extends MapReduceBase implements Mapper {  
  9.   
  10.     public void map(WritableComparable key, Writable values,  
  11.             OutputCollector output, Reporter reporter) throws IOException {  
  12.     }  
  13. }  

新建Reducer的向导图为:


生成的Reducer代码如下:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. import java.io.IOException;  
  2. import java.util.Iterator;  
  3. import org.apache.hadoop.io.WritableComparable;  
  4. import org.apache.hadoop.mapred.MapReduceBase;  
  5. import org.apache.hadoop.mapred.OutputCollector;  
  6. import org.apache.hadoop.mapred.Reducer;  
  7. import org.apache.hadoop.mapred.Reporter;  
  8. public class Reducer1 extends MapReduceBase implements Reducer {  
  9.     public void reduce(WritableComparable _key, Iterator values,  
  10.             OutputCollector output, Reporter reporter) throws IOException {  
  11.         // replace KeyType with the real type of your key  
  12.         KeyType key = (KeyType) _key;  
  13.         while (values.hasNext()) {  
  14.             // replace ValueType with the real type of your value  
  15.             ValueType value = (ValueType) values.next();  
  16.             // process value  
  17.         }  
  18.     }  
  19. }  

新建Driver的向导图为:


生成的Driver代码为:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. import org.apache.hadoop.fs.Path;  
  2. import org.apache.hadoop.io.IntWritable;  
  3. import org.apache.hadoop.io.Text;  
  4. import org.apache.hadoop.mapred.JobClient;  
  5. import org.apache.hadoop.mapred.JobConf;  
  6. import org.apache.hadoop.mapred.Mapper;  
  7. import org.apache.hadoop.mapred.Reducer;  
  8. public class Driver1 {  
  9.     public static void main(String[] args) {  
  10.         JobClient client = new JobClient();  
  11.         JobConf conf = new JobConf(Driver1.class);  
  12.         // TODO: specify output types  
  13.         conf.setOutputKeyClass(Text.class);  
  14.         conf.setOutputValueClass(IntWritable.class);  
  15.         // TODO: specify input and output DIRECTORIES (not files)  
  16.         conf.setInputPath(new Path("src"));  
  17.         conf.setOutputPath(new Path("out"));  
  18.         // TODO: specify a mapper  
  19.     conf.setMapperClass(org.apache.hadoop.mapred.lib.IdentityMapper.class);  
  20.         // TODO: specify a reducer  
  21.     conf.setReducerClass(org.apache.hadoop.mapred.lib.IdentityReducer.class);  
  22.         client.setConf(conf);  
  23.         try {  
  24.             JobClient.runJob(conf);  
  25.         } catch (Exception e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.     }  
  29. }  

在看过了未作修改时的Mapper和Reducer的向导和生成的代码后,现在看看修改后的效果。下面是新的Mapper向导:


生成的新Mapper代码为:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. import java.io.IOException;  
  2. import org.apache.hadoop.io.Writable;  
  3. import org.apache.hadoop.io.WritableComparable;  
  4. import org.apache.hadoop.mapreduce.Mapper;  
  5. public class Mapper2 extends Mapper {  
  6.     public void map(WritableComparable key, Writable value, Context context)  
  7.             throws IOException, InterruptedException {  
  8.     }  
  9. }  

新的Reducer向导如下:


生成的新Reducer代码为:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. import java.io.IOException;  
  2. import org.apache.hadoop.io.Writable;  
  3. import org.apache.hadoop.io.WritableComparable;  
  4. import org.apache.hadoop.mapreduce.Reducer;  
  5. public class Reducer2 extends Reducer {  
  6.     public void reduce(WritableComparable key, Iterable<Writable> values,  
  7.             Context context) throws IOException, InterruptedException {  
  8.     }  
  9. }  

新的Driver向导如下:


生成的新Driver代码为:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. import org.apache.hadoop.fs.Path;  
  2. import org.apache.hadoop.io.IntWritable;  
  3. import org.apache.hadoop.io.Text;  
  4. import org.apache.hadoop.mapreduce.Mapper;  
  5. import org.apache.hadoop.mapreduce.Reducer;  
  6. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  7. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  8. public class Driver2 {  
  9.     public static void main(String[] args) throws Exception {  
  10.         Configuration conf = new Configuration();  
  11.         Job job = new Job(conf, "Your job name");  
  12.         // TODO: specify output types  
  13.         job.setOutputKeyClass(Text.class);  
  14.         job.setOutputValueClass(IntWritable.class);  
  15.         // TODO: specify a mapper  
  16.         job.setMapperClass(org.apache.hadoop.mapred.lib.IdentityMapper.class);  
  17.         // TODO: specify a reducer  
  18.       job.setReducerClass(org.apache.hadoop.mapred.lib.IdentityReducer.class);  
  19.         FileInputFormat.addInputPath(job, new Path(args[0]));  
  20.         FileOutputFormat.setOutputPath(job, new Path(args[1]));  
  21.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  22.     }  
  23. }  

在看过了实际效果后,接下来就是如何具体修改源代码,修改主要涉及到NewMapperWizard.java、NewReducerWizard.java、NewDriverWizardPage.java这三个文件。按照上面展示Mapper、Reducer、Driver的顺序,先看看Mapper的修改,在NewMapperWizard.java中修改createTypeMembers和createControl方法中的部分方法,其中createTypeMembers方法中的代码改为:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. super.createTypeMembers(newType, imports, monitor);  
  2. imports.addImport("java.io.IOException");  
  3. imports.addImport("org.apache.hadoop.io.WritableComparable");  
  4. imports.addImport("org.apache.hadoop.io.Writable");  
  5. //imports.addImport("org.apache.hadoop.mapred.OutputCollector");  
  6. //imports.addImport("org.apache.hadoop.mapred.Reporter");  
  7. /*newType 
  8.     .createMethod( 
  9.         "public void map(WritableComparable key, Writable values, OutputCollector output, Reporter reporter) throws IOException \n{\n}\n", 
  10.         null, false, monitor);*/  
  11. newType  
  12.     .createMethod(  
  13.         "public void map(WritableComparable key, Writable value, Context context) throws IOException, InterruptedException {\n}\n",  
  14.         nullfalse, monitor);  

createControl方法中的代码修改部分为:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //setSuperClass("org.apache.hadoop.mapred.MapReduceBase", true);  
  2. setSuperClass("org.apache.hadoop.mapreduce.Mapper"true);   
  3. // setSuperInterfaces(Arrays  
  4. //    .asList(new String[] { "org.apache.hadoop.mapred.Mapper" }), true);  

NewReducerWizard.java中的修改与Mapper中的修改基本类似,也是修改createTypeMembers和createControl中的代码,具体createTypeMembers为:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1.    super.createTypeMembers(newType, imports, monitor);  
  2.    imports.addImport("java.io.IOException");  
  3.    imports.addImport("org.apache.hadoop.io.WritableComparable");  
  4. imports.addImport("org.apache.hadoop.io.Writable");  
  5.    //imports.addImport("org.apache.hadoop.mapred.OutputCollector");  
  6.    //imports.addImport("org.apache.hadoop.mapred.Reporter");  
  7.    /*newType 
  8.        .createMethod( 
  9.            "public void reduce(WritableComparable _key, Iterable values, OutputCollector output, Reporter reporter) throws IOException \n{\n" 
  10.                + "\t// replace KeyType with the real type of your key\n" 
  11.                + "\tKeyType key = (KeyType) _key;\n\n" 
  12.                + "\twhile (values.hasNext()) {\n" 
  13.                + "\t\t// replace ValueType with the real type of your value\n" 
  14.                + "\t\tValueType value = (ValueType) values.next();\n\n" 
  15.                + "\t\t// process value\n" + "\t}\n" + "}\n", null, false, 
  16.            monitor);*/  
  17. newType  
  18.        .createMethod(  
  19.            "public void reduce(WritableComparable key, Iterable<Writable> values, Context context) throws IOException, InterruptedException {\n}\n",   
  20.   nullfalse,  
  21.            monitor);      

createControl方法的修改为:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //setSuperClass("org.apache.hadoop.mapred.MapReduceBase", true);  
  2. /*setSuperInterfaces(Arrays 
  3.     .asList(new String[] { "org.apache.hadoop.mapred.Reducer" }), true);*/  
  4. SuperClass("org.apache.hadoop.mapreduce.Reducer"true);  

NewDriverWizardPage.java文件主要修改的方法也为createTypeMembers和createControl,其中createTypeMembers方法修改为:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /*String method = "public static void main(String[] args) {\n JobClient client = new JobClient();"; 
  2.         method += "JobConf conf = new JobConf(" 
  3.             + newType.getFullyQualifiedName() + ".class);\n\n"; 
  4.  
  5.         method += "// TODO: specify output types\nconf.setOutputKeyClass(Text.class);\nconf.setOutputValueClass(IntWritable.class);\n\n"; 
  6.  
  7.         method += "// TODO: specify input and output DIRECTORIES (not files)\nconf.setInputPath(new Path(\"src\"));\nconf.setOutputPath(new Path(\"out\"));\n\n"; 
  8.  
  9.         if (mapperText.getText().length() > 0) { 
  10.           method += "conf.setMapperClass(" + mapperText.getText() 
  11.               + ".class);\n\n"; 
  12.         } else { 
  13.           method += "// TODO: specify a mapper\nconf.setMapperClass(org.apache.hadoop.mapred.lib.IdentityMapper.class);\n\n"; 
  14.         } 
  15.         if (reducerText.getText().length() > 0) { 
  16.           method += "conf.setReducerClass(" + reducerText.getText() 
  17.               + ".class);\n\n"; 
  18.         } else { 
  19.           method += "// TODO: specify a reducer\nconf.setReducerClass(org.apache.hadoop.mapred.lib.IdentityReducer.class);\n\n"; 
  20.         } 
  21.  
  22.         method += "client.setConf(conf);\n"; 
  23.         method += "try {\n\tJobClient.runJob(conf);\n} catch (Exception e) {\n" 
  24.             + "\te.printStackTrace();\n}\n"; 
  25.         method += "}\n";*/  
  26.     String method = "public static void main(String[] args) throws Exception {\n  Configuration conf = new Configuration();\n";  
  27.         method += "Job job = new Job(conf, \"Your job name\");\n";  
  28.   
  29.         method += "// TODO: specify output types\njob.setOutputKeyClass(Text.class);\njob.setOutputValueClass(IntWritable.class);\n\n";  
  30.   
  31.         //method += "// TODO: specify input and output DIRECTORIES (not files)\nconf.setInputPath(new Path(\"src\"));\nconf.setOutputPath(new Path(\"out\"));\n\n";  
  32.   
  33.         if (mapperText.getText().length() > 0) {  
  34.           method += "job.setMapperClass(" + mapperText.getText()  
  35.               + ".class);\n\n";  
  36.         } else {  
  37.           method += "// TODO: specify a mapper\njob.setMapperClass(org.apache.hadoop.mapred.lib.IdentityMapper.class);\n\n";  
  38.         }  
  39.         if (reducerText.getText().length() > 0) {  
  40.           method += "job.setReducerClass(" + reducerText.getText()  
  41.               + ".class);\n\n";  
  42.         } else {  
  43.           method += "// TODO: specify a reducer\njob.setReducerClass(org.apache.hadoop.mapred.lib.IdentityReducer.class);\n\n";  
  44.         }  
  45.         method += "FileInputFormat.addInputPath(job, new Path(args[0]));\n";  
  46.         method += "FileOutputFormat.setOutputPath(job, new Path(args[1]));\n";  
  47.         method += "System.exit(job.waitForCompletion(true) ? 0 : 1);\n";  
  48.         method += "}\n";  

createControl方法中主要是注释掉:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. // setSuperClass("org.apache.hadoop.mapred.MapReduceBase", true);  
  2.     // setSuperInterfaces(Arrays.asList(new String[]{  
  3.     // "org.apache.hadoop.mapred.Mapper" }), true);  

这样Eclipse的Hadoop-1.2.1插件已经修改完毕,重新编译一下,并将编译成功的jar文件放到Eclipse的plugins目录中并重启Eclipse就可以达到上面描述过的效果。

0 0
原创粉丝点击