分享

hadoop2.x如何通过eclipse执行mapreduce

zhangcd123 发表于 2014-4-29 15:10:13 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 2 7097
请问有谁用过eclipse执行hadoop2.2的MapReduce吗?有的话能提供个例子吗?不要用hadoop jar执行

已有(2)人评论

跳转到指定楼层
sstutu 发表于 2014-4-29 17:05:52

在hadoop-2.2.0.tar.gz文件下没有找到源码(新版本不但没有Eclipse插件,也没有源码,只有.class字节码文件),可以下载hadoop-2.2.0-src.tar.gz,解压,然后在hadoop-mapreduce-examples/src/main/java/org/apache/hadoop/examples目录下获取源码。
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements.  See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership.  The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License.  You may obtain a copy of the License at
  9. *
  10. *     http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */  
  18. package org.apache.hadoop.examples;  
  19.   
  20. import java.io.IOException;  
  21. import java.util.StringTokenizer;  
  22.   
  23. import org.apache.hadoop.conf.Configuration;  
  24. import org.apache.hadoop.fs.Path;  
  25. import org.apache.hadoop.io.IntWritable;  
  26. import org.apache.hadoop.io.Text;  
  27. import org.apache.hadoop.mapreduce.Job;  
  28. import org.apache.hadoop.mapreduce.Mapper;  
  29. import org.apache.hadoop.mapreduce.Reducer;  
  30. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  31. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  32. import org.apache.hadoop.util.GenericOptionsParser;  
  33.   
  34. public class WordCount {  
  35.   
  36.   public static class TokenizerMapper   
  37.        extends Mapper<Object, Text, Text, IntWritable>{  
  38.       
  39.     private final static IntWritable one = new IntWritable(1);  
  40.     private Text word = new Text();  
  41.     // value已经是文件内容的一行  
  42.     public void map(Object key, Text value, Context context  
  43.                     ) throws IOException, InterruptedException {  
  44.       StringTokenizer itr = new StringTokenizer(value.toString());  
  45.       while (itr.hasMoreTokens()) {  
  46.         word.set(itr.nextToken());  
  47.         context.write(word, one);  
  48.       }  
  49.     }  
  50.   }  
  51.    
  52.   public static class IntSumReducer   
  53.        extends Reducer<Text,IntWritable,Text,IntWritable> {  
  54.     private IntWritable result = new IntWritable();  
  55.   
  56.     public void reduce(Text key, Iterable<IntWritable> values,   
  57.                        Context context  
  58.                        ) throws IOException, InterruptedException {  
  59.       int sum = 0;  
  60.       for (IntWritable val : values) {  
  61.         sum += val.get();  
  62.       }  
  63.       result.set(sum);  
  64.       context.write(key, result);  
  65.     }  
  66.   }  
  67.   
  68.   public static void main(String[] args) throws Exception {  
  69.     Configuration conf = new Configuration();  
  70.     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();  
  71.     if (otherArgs.length != 2) {  
  72.       System.err.println("Usage: wordcount <in> <out>");  
  73.       System.exit(2);  
  74.     }  
  75.     Job job = new Job(conf, "word count");  
  76.     job.setJarByClass(WordCount.class);  
  77.     job.setMapperClass(TokenizerMapper.class);  
  78.     job.setCombinerClass(IntSumReducer.class);  
  79.     job.setReducerClass(IntSumReducer.class);  
  80.     job.setOutputKeyClass(Text.class);  
  81.     job.setOutputValueClass(IntWritable.class);  
  82.     FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  83.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  84.     System.exit(job.waitForCompletion(true) ? 0 : 1);  
  85.   }  
  86. }  
复制代码

在Eclipse中创建一个MapReduce Project,然后新建Java类,例如创建一个MyWordCount 类,然后将WordCount.java程序代码拷贝到MyWordCount.java文件中。然后点击Run-->Run Configurations…,在弹出的对话框中左边栏选择Java Application,选中MyWordCount,在右边栏中对Arguments进行配置。
在Program arguments中配置输入输出目录参数
/home/jack/Desktop/in /home/jack/Desktop/out
在VM arguments中配置VM arguments的参数
-Xms512m -Xmx1024m -XX:MaxPermSize=256m
注:
  • in文件夹是需要在程序运行前创建的,并且要放入需要统计词频的文件,out文件夹是不能提前创建的,要由系统自动生成,否则运行时会出现Output directory file:/home/jack/Desktop/out already exists错误。
  • 文件输入和输出目录为本地文件系统中的文件。
  • 程序运行需要点击菜单栏上的Run。
  • 程序运行结束后,可以在/home/jack/Desktop/out目录下的part-r-00000文件查看到词频统计的结果。


回复

使用道具 举报

nettman 发表于 2014-4-29 20:20:53
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条