在Eclipse中配置Hadoop2.6.0

1、下载并配置插件

将下载的hadoop-eclipse-plugin-2.6.0.jar 放到Eclipse的dropins目录下,重启Eclipse.

2、配置 hadoop 安装目录

  • window ->preference -> hadoop Map/Reduce -> Hadoop installation directory

3、配置Map/Reduce 视图

  • window ->Open Perspective -> other->Map/Reduce -> 点击“OK”
  • windows → show view → other->Map/Reduce Locations-> 点击“OK”
  • 控制台会多出一个“Map/Reduce Locations”的Tab页

在“Map/Reduce Locations” Tab页 点击图标<大象+>或者在空白的地方右键,选择“New Hadoop location…”,弹出对话框“New hadoop location…”,配置如下内容:将ha1改为自己的hadoop用户

注意:MR Master和DFS Master配置必须和mapred-site.xml和core-site.xml等配置文件一致

打开Project Explorer,查看HDFS文件系统。

4、新建Map/Reduce任务

File->New->project->Map/Reduce Project->Next

编写WordCount类:记得先把服务都起来

[java] view plaincopy
 
  1. import java.io.IOException;  
  2. import java.util.*;  
  3. import org.apache.hadoop.fs.Path;  
  4. import org.apache.hadoop.conf.*;  
  5. import org.apache.hadoop.io.*;  
  6. import org.apache.hadoop.mapred.*;  
  7. import org.apache.hadoop.util.*;  
  8. public class WordCount {  
  9. public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {  
  10. private final static IntWritable one = new IntWritable(1);  
  11. private Text word = new Text();  
  12.   
  13. public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {  
  14.  String line = value.toString();  
  15.  StringTokenizer tokenizer = new StringTokenizer(line);  
  16.  while (tokenizer.hasMoreTokens()) {  
  17.    word.set(tokenizer.nextToken());  
  18.    output.collect(word, one);  
  19.  }  
  20. }  
  21. }  
  22. public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {  
  23. public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {  
  24.  int sum = 0;  
  25.  while (values.hasNext()) {  
  26.    sum += values.next().get();  
  27.  }  
  28.  output.collect(key, new IntWritable(sum));  
  29. }  
  30. }  
  31. public static void main(String[] args) throws Exception {  
  32. JobConf conf = new JobConf(WordCount.class);  
  33. conf.setJobName("wordcount");  
  34.   
  35. conf.setOutputKeyClass(Text.class);  
  36. conf.setOutputValueClass(IntWritable.class);  
  37.   
  38. conf.setMapperClass(Map.class);  
  39. conf.setReducerClass(Reduce.class);  
  40.   
  41. conf.setInputFormat(TextInputFormat.class);  
  42. conf.setOutputFormat(TextOutputFormat.class);  
  43.   
  44. FileInputFormat.setInputPaths(conf, new Path(args[0]));  
  45. FileOutputFormat.setOutputPath(conf, new Path(args[1]));  
  46.   
  47. JobClient.runJob(conf);  
  48. }  
  49. }  

5、配置运行时参数:右键-->Run as-->Run Confiugrations

user/ha1/input/hadoop是你上传在hdfs的文件夹(自己创建),里面放要处理的文件。ouput4放输出结果

将程序放在hadoop集群上运行:右键-->Runas -->Run on Hadoop,最终的输出结果会在HDFS相应的文件夹下显示。至此,ubuntu下hadoop-2.6.0 eclipse插件配置完成。

 

特别注意:按以上配置后,执行MR控制台没有日志详细信息。

解决方案,将hadoop-2.6.0/etc/haoop/log4j.properties 文件拷贝到 Eclipse项目下即可 (例如,将其拷贝到/home/hadoop/workspace/WordCount路径下<疑惑:在Linux文件系统中拷贝到该路径无效,但是从Eclipse视图中拷贝就有效。。且上述路径中相应的生成log4j.properties 文件>)。

 

 

http://blog.csdn.net/zythy/article/details/17397153

posted @ 2015-10-23 17:11  MERRU  阅读(961)  评论(0编辑  收藏  举报