手机版
你好,游客 登录 注册
背景:
阅读新闻

Hadoop1.0 Eclipse Plugin-作业提交

[日期:2012-07-30] 来源:Linux社区  作者:java2000_wl [字体:大 中 小]

1.环境

Jdk:1.6.0_10-rc2

Hadoop:hadoop-1.0.0.tar.gz

Eclipse 版本:3.4.0

Hadoop Eclipse 插件 :hadoop-eclipse-plugin-1.0.0.jar   下载地址

操作系统:Windows7 32位 旗舰版

2.Eclipse插件配置

2.1   把"hadoop-eclipse-plugin-1.0.0.jar"放到Eclipse的目录的"plugins"中(eclipse/plugins),重新启动Eclipse生效

2.2   选择Elipse Window菜单下的"Preference",配置"Hadoop Map/Reduce"选项,选择Hadoop的安装根目录

2.3 配置Hadoop Location

在配置Hadoop Location之前 确定hadoop 已启动起来

Eclipse 切换到“Map/Reduce Locations” 视图 , 在"Map/Reduce Locations"视图右击 选择"New Hadoop Location",

* Map/Reduce Master与mapred-site.xml配置文件对应

* DFS Mast 与core-site.xml配置对应

创建完成后 ,切换到JavaEE视图    刷新右边的DFS Locations 就会看到dfs文件结构

可以在节点上右键  创建 删除目录做测试

3.运行wordCount例子程序

创建一个 Map/Reduce Project项目

创建成功后  WordCount报名对应(源码在hadoop\src\examples\org\apache\hadoop\examples目录下)

WordCount.java

  1. package org.apache.hadoop.examples;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.URI;  
  5. import java.util.StringTokenizer;  
  6.   
  7. import org.apache.hadoop.conf.Configuration;  
  8. import org.apache.hadoop.fs.FSDataInputStream;  
  9. import org.apache.hadoop.fs.FileSystem;  
  10. import org.apache.hadoop.fs.FileUtil;  
  11. import org.apache.hadoop.fs.Path;  
  12. import org.apache.hadoop.io.IOUtils;  
  13. import org.apache.hadoop.io.IntWritable;  
  14. import org.apache.hadoop.io.Text;  
  15. import org.apache.hadoop.mapreduce.Job;  
  16. import org.apache.hadoop.mapreduce.Mapper;  
  17. import org.apache.hadoop.mapreduce.Reducer;  
  18. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  19. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  20. import org.apache.hadoop.util.GenericOptionsParser;  
  21.   
  22. public class WordCount {  
  23.   
  24.   public static class TokenizerMapper   
  25.        extends Mapper<Object, Text, Text, IntWritable>{  
  26.       
  27.     private final static IntWritable one = new IntWritable(1);  
  28.     private Text word = new Text();  
  29.         
  30.     public void map(Object key, Text value, Context context  
  31.                     ) throws IOException, InterruptedException {  
  32.       StringTokenizer itr = new StringTokenizer(value.toString());  
  33.       while (itr.hasMoreTokens()) {  
  34.         word.set(itr.nextToken());  
  35.         context.write(word, one);  
  36.       }  
  37.     }  
  38.   }  
  39.     
  40.   public static class IntSumReducer   
  41.        extends Reducer<Text,IntWritable,Text,IntWritable> {  
  42.     private IntWritable result = new IntWritable();  
  43.     public void reduce(Text key, Iterable<IntWritable> values,   
  44.                        Context context  
  45.                        ) throws IOException, InterruptedException {  
  46.       int sum = 0;  
  47.       for (IntWritable val : values) {  
  48.         sum += val.get();  
  49.       }  
  50.       result.set(sum);  
  51.       context.write(key, result);  
  52.     }  
  53.   }  
  54.   
  55.   public static void main(String[] args) throws Exception {  
  56.     Configuration conf = new Configuration();  
  57.     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();  
  58.     if (otherArgs.length != 2) {  
  59.       System.err.println("Usage: wordcount <in> <out>");  
  60.       System.exit(2);  
  61.     }  
  62.     //删除输出目录   
  63.     FileSystem fileSystem = FileSystem.get(URI.create(args[1]),conf);  
  64.     fileSystem.delete(new Path("/user/admin/output"), true);  
  65.       
  66.     Job job = new Job(conf, "word count");  
  67.     job.setJarByClass(WordCount.class);  
  68.     job.setMapperClass(TokenizerMapper.class);  
  69.     job.setCombinerClass(IntSumReducer.class);  
  70.     job.setReducerClass(IntSumReducer.class);  
  71.     job.setOutputKeyClass(Text.class);  
  72.     job.setOutputValueClass(IntWritable.class);  
  73.     FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  74.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  75.     boolean flag = job.waitForCompletion(true);  
  76.     FSDataInputStream  fDataInputStream = null;  
  77.     try {  
  78.         fDataInputStream = fileSystem.open(new Path("/user/admin/output/part-r-00000"));  
  79.         String line = null;  
  80.         while ((line = fDataInputStream.readLine()) != null) {  
  81.             System.out.println(line);;  
  82.         }  
  83.     } catch (Exception e) {  
  84.         e.printStackTrace();  
  85.     } finally {  
  86.         IOUtils.closeStream(fDataInputStream);  
  87.         IOUtils.closeStream(fileSystem);  
  88.     }  
  89.     System.exit( flag ? 0 : 1);  
  90.   }  
  91. }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款