`
wbj0110
  • 浏览: 1549627 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

eclipse中开发Hadoop2.x的Map/Reduce项目汇总

阅读更多
问题导读:
1.如何创建MR程序?
2.如何配置运行参数?
3.HADOOP_HOME为空会出现什么问题?
4.hadoop-common-2.2.0-bin-master/bin的作用是什么?
扩展:
4.winutils.exe是什么?







本文总结了两个例子,分别从不同角度。
一、eclipse中开发Hadoop2.x的Map/Reduce项目
本文演示如何在Eclipse中开发一个Map/Reduce项目:
1、环境说明
2、新建MR工程
依次点击 File → New → Ohter…  选择 “Map/Reduce Project”,然后输入项目名称:micmiu_MRDemo,创建新项目:
<ignore_js_op style="word-wrap: break-word;"> 

 

<ignore_js_op style="word-wrap: break-word;"> 
3、创建Mapper和Reducer
依次点击 File → New → Ohter… 选择Mapper,自动继承Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>
<ignore_js_op style="word-wrap: break-word;"> 
<ignore_js_op style="word-wrap: break-word;"> 
创建Reducer的过程同Mapper,具体的业务逻辑自己实现即可。
本文就以官方自带的WordCount为例进行测试:

 

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



4、准备测试数据
micmiu-01.txt:

 

  1. Hi Michael welcome to Hadoop 
  2. more see micmiu.com
复制代码


micmiu-02.txt:

  1. Hi Michael welcome to BigData
  2. more see micmiu.com
复制代码


micmiu-03.txt:

  1. Hi Michael welcome to Spark 
  2. more see micmiu.com
复制代码


把 micmiu 打头的三个文件上传到hdfs:

  1. micmiu-mbp:Downloads micmiu$ hdfs dfs -copyFromLocal micmiu-*.txt /user/micmiu/test/input
  2. micmiu-mbp:Downloads micmiu$ hdfs dfs -ls /user/micmiu/test/input
  3. Found 3 items
  4. -rw-r--r--   1 micmiu supergroup         50 2014-04-15 14:53 /user/micmiu/test/input/micmiu-01.txt
  5. -rw-r--r--   1 micmiu supergroup         50 2014-04-15 14:53 /user/micmiu/test/input/micmiu-02.txt
  6. -rw-r--r--   1 micmiu supergroup         49 2014-04-15 14:53 /user/micmiu/test/input/micmiu-03.txt
  7. micmiu-mbp:Downloads micmiu$
复制代码



5、配置运行参数
Run As → Run Configurations… ,在Arguments中配置运行参数,例如程序的输入参数:
<ignore_js_op style="word-wrap: break-word;"> 
6、运行
Run As -> Run on Hadoop ,执行完成后可以看到如下信息:
<ignore_js_op style="word-wrap: break-word;"> 
到此Eclipse中调用Hadoop2x本地伪分布式模式执行MR演示成功。
ps:调用集群环境MR运行一直失败,暂时没有找到原因。


上面说了一个整体的过程,下面详细描述了遇到的问题

二、Win7 Eclipse调试Centos Hadoop2.2-Mapreduce

 

1.搭建了一套Centos5.3 + Hadoop2.2 + Hbase0.96.1.1的开发环境,Win7 Eclipse调试MapReduce成功。

2. Hadoop安装
安装成功后,能顺利查看以下几个页面,就OK了。我的集群环境是200master,201-203slave。
dfs.http.address   192.168.1.200:50070
dfs.secondary.http.address  192.168.1.200:50090
dfs.datanode.http.address  192.168.1.201:50075
yarn.resourcemanager.webapp.address  192.168.1.200:50030
mapreduce.jobhistory.webapp.address 192.168.1.200:19888。这个好像访问不了。需要启动hadoop/sbin/mr-jobhistory-daemon.sh start historyserver才可以访问。
三. Hadoop2.x eclispe-plugin
需要注意一点的是,Hadoop installation directory里填写Win下的hadoop home地址,其目的在于创建MapReduce Project能从这个地方自动引入MapReduce需要的jar。
插件可以从下面下载:
四. 各种问题
1.上面一步完成后,创建一个MapReduce Project,运行时发现出问题了。
  1. java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
复制代码

跟代码就去发现是HADOOP_HOME的问题。如果HADOOP_HOME为空,必然fullExeName为null\bin\winutils.exe。解决方法很简单啦,乖乖的配置环境变量吧,不想重启电脑可以在MapReduce程序里加上System.setProperty("hadoop.home.dir", "...");暂时缓缓。org.apache.hadoop.util.Shell.java
  1.   public static final String getQualifiedBinPath(String executable) 
  2.   throws IOException {
  3.     // construct hadoop bin path to the specified executable
  4.     String fullExeName = HADOOP_HOME_DIR + File.separator + "bin" 
  5.       + File.separator + executable;
  6.     File exeFile = new File(fullExeName);
  7.     if (!exeFile.exists()) {
  8.       throw new IOException("Could not locate executable " + fullExeName
  9.         + " in the Hadoop binaries.");
  10.     }
  11.     return exeFile.getCanonicalPath();
  12.   }
  13. private static String HADOOP_HOME_DIR = checkHadoopHome();
  14. private static String checkHadoopHome() {
  15.     // first check the Dflag hadoop.home.dir with JVM scope
  16.     String home = System.getProperty("hadoop.home.dir");
  17.     // fall back to the system/user-global env variable
  18.     if (home == null) {
  19.       home = System.getenv("HADOOP_HOME");
  20.     }
  21.      ...
  22. }
复制代码

2.这个时候得到完整的地址fullExeName,我机器上是D:\Hadoop\tar\hadoop-2.2.0\hadoop-2.2.0\bin\winutils.exe。继续执行代码又发现了错误
  1. Could not locate executable D:\Hadoop\tar\hadoop-2.2.0\hadoop-2.2.0\bin\winutils.exe in the Hadoop binaries.
复制代码

就去一看,没有winutils.exe这个东西。去https://github.com/srccodes/hadoop-common-2.2.0-bin下载一个,放就去即可。
3.继续出问题
  1. at org.apache.hadoop.util.Shell.execCommand(Shell.java:661)
  2. at org.apache.hadoop.fs.RawLocalFileSystem.setPermission(RawLocalFileSystem.java:639)
  3. at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:435)
复制代码
继续跟代码org.apache.hadoop.util.Shell.java
  1.   public static String[] getSetPermissionCommand(String perm, boolean recursive,
  2.                                                  String file) {
  3.     String[] baseCmd = getSetPermissionCommand(perm, recursive);
  4.     String[] cmdWithFile = Arrays.copyOf(baseCmd, baseCmd.length + 1);
  5.     cmdWithFile[cmdWithFile.length - 1] = file;
  6.     return cmdWithFile;
  7.   }
  8.   /** Return a command to set permission */
  9.   public static String[] getSetPermissionCommand(String perm, boolean recursive) {
  10.     if (recursive) {
  11.       return (WINDOWS) ? new String[] { WINUTILS, "chmod", "-R", perm }
  12.                          : new String[] { "chmod", "-R", perm };
  13.     } else {
  14.       return (WINDOWS) ? new String[] { WINUTILS, "chmod", perm }
  15.                        : new String[] { "chmod", perm };
  16.     }
  17.   }
复制代码

cmdWithFile数组的内容为{"D:\Hadoop\tar\hadoop-2.2.0\hadoop-2.2.0\bin\winutils.exe", "chmod", "755", "xxxfile"},我把这个单独在cmd里执行了一下,发现
无法启动此程序,因为计算机中丢失 MSVCR100.dll  

那就下载一个呗http://files.cnblogs.com/sirkevin/msvcr100.rar,丢到C:\Windows\System32里面。再次cmd执行,又来了问题
应用程序无法正常启动(0xc000007b)


下载http://blog.csdn.net/vbcom/article/details/7245186 ,DirectX_Repair来解决这个问题吧。记得修复完后要重启电脑。搞定后cmd试一下,很棒。
4.到了这里,已经看到曙光了,但问题又来了
  1. Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z
复制代码

代码就去
  1.     /** Windows only method used to check if the current process has requested
  2.      *  access rights on the given path. */
  3.     private static native boolean access0(String path, int requestedAccess);
复制代码

显然缺少dll文件,还记得https://github.com/srccodes/hadoop-common-2.2.0-bin下载的东西吧,里面就有hadoop.dll,最好的方法就是用hadoop-common-2.2.0-bin-master/bin目录替换本地hadoop的bin目录,并在环境变量里配置PATH=HADOOP_HOME/bin,重启电脑。
5.终于看到了MapReduce的正确输出output99。
<ignore_js_op style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 14px; line-height: 21px;"> 

五. 总结 
hadoop eclipse插件不是必须的,其作用在我看来就是如下三点(这个是一个错误的认识,具体请参考http://zy19982004.iteye.com/blog/2031172)。study-hadoop是一个普通project,直接运行(不通过Run on Hadoop这只大象),一样可以调试到MapReduce。
对hadoop中的文件可视化。
创建MapReduce Project时帮你引入依赖的jar。
Configuration conf = new Configuration();时就已经包含了所有的配置信息。
还是自己下载hadoop2.2的源码编译好,应该是不会有任何问题的(没有亲测)。


六. 其它问题

1.还是

  1. Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z
复制代码


代码跟到org.apache.hadoop.util.NativeCodeLoader.java去看

  1.   static {
  2.     // Try to load native hadoop library and set fallback flag appropriately
  3.     if(LOG.isDebugEnabled()) {
  4.       LOG.debug("Trying to load the custom-built native-hadoop library...");
  5.     }
  6.     try {
  7.       System.loadLibrary("hadoop");
  8.       LOG.debug("Loaded the native-hadoop library");
  9.       nativeCodeLoaded = true;
  10.     } catch (Throwable t) {
  11.       // Ignore failure to load
  12.       if(LOG.isDebugEnabled()) {
  13.         LOG.debug("Failed to load native-hadoop with error: " + t);
  14.         LOG.debug("java.library.path=" +
  15.             System.getProperty("java.library.path"));
  16.       }
  17.     }
  18.     
  19.     if (!nativeCodeLoaded) {
  20.       LOG.warn("Unable to load native-hadoop library for your platform... " +
  21.                "using builtin-java classes where applicable");
  22.     }
  23.   }
复制代码


这里报错如下

  1. DEBUG org.apache.hadoop.util.NativeCodeLoader - Failed to load native-hadoop with error: java.lang.UnsatisfiedLinkError: HADOOP_HOME\bin\hadoop.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform
复制代码


怀疑是32位jdk的问题,替换成64位后,没问题了

  1. 2014-03-11 19:43:08,805 DEBUG org.apache.hadoop.util.NativeCodeLoader - Trying to load the custom-built native-hadoop library...
  2. 2014-03-11 19:43:08,812 DEBUG org.apache.hadoop.util.NativeCodeLoader - Loaded the native-hadoop library
复制代码


这里也解决了一个常见的警告

  1. WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
复制代码

http://www.aboutyun.com/thread-7541-1-1.html

分享到:
评论

相关推荐

    hadoop中map/reduce

    hadoop中map/reduce自学资料合集

    Windows平台下Hadoop的Map/Reduce开发

    讲述了Windows平台的Hadoop安装,以及在Eclipse中环境搭建。针对Windows平台搭建Hadoop,给出了详细步骤。最难得的是,详细给出了Windows平台的Hadoop安装常见问题及解决方案。 最后,以最简单的求和为例,剖析...

    hadoop-eclipse-plugin-2.7.3.jar

    放到eclipse的plugins目录下。重启eclipse ...配置 Map/Reduce Master和DFS Mastrer,Host和Port配置成hdfs-site.xml与core-site.xml的设置一致即可。  如果连接成功,会出现hdfs上面的文件夹

    eclipse-hadoop3x-master.rar

    大数据是当今的一个热门话题,相信搞JAVA的我们当然不能错过了,尤其是...hadoop可以不用eclipse插件也可以运行,但当然,对于我们初学的,有个插件肯定好很多,我们可以集中精力先让它跑起来,然后再慢慢去深入研究。

    hadoop-eclipse-plugin-3.2.1.jar

    hadoop-eclipse-plugin.jar插件基于Ubuntu18.04和Hadoop-3.2.1编译的,最后可以在eclipse创建Map Reduce文件

    hadoop-eclipse-2.7.6.jar

    hadoop-eclipse-2.76 eclipse4.5的集成开发map/reduce.经测试可行。博文地址:https://blog.csdn.net/nengyu/article/details/83998176

    hadoop-eclipse-plugins

    eclipse中使用Hadoop Map/Reduce插件进行map/reduce的开发

    hadoop2.9.2 涉及的mr编程相关文件.rar

    2.把windows运行Hadoop的一个系统文件hadoop.dll放到C:\Windows\System32中 3.将hadoop-eclipse-plugin-2.8.5.jar放到eclipse的插件文件夹内(我的: E:\cxj\eclipse\plugins) 4.启动eclipse,检查插件是否运行...

    大数据云计算技术系列 hadoop搭建与eclipse开发环境设置-已验证通过(共13页).pdf

    重启eclipse,打开windows-&gt;open perspective-&gt;other-&gt;map/reduce 可以看到map/reduce开发视图。 1.2 设置连接参数 打开windows-&gt;show view-&gt;other-&gt; map/reduce Locations视图,在点击大象后弹出的对话框(General ...

    hadoop-eclipse-plugin-2.8.0.zip 亲测Eclipse2020+Hadoop2.10可用

    将文档拷贝到eclipse\plugins目录下,重启eclipse,可见 Hadoop Map/Reduce小象选项

    基于Hadoop的数据分析.doc

    设置eclipse的Java环境:在eclipse下创建一个jre文件夹sudo mkdir jre,进 " "入该文件夹:cd jre, " "加入一条连接指令ln -s /usr/lib/jvm/java-7-sun/bin bin " "(2)在eclipse中添加Map/Reduce:点击上方window...

    Map-Reduce原理体系架构和工作机制,eclipse与Hadoop集群连接

    Map-Reduce原理体系架构和工作机制,eclipse与Hadoop集群连接

    Hadoop-Eclipse-Plugin-2.7.3

    Hadoop-Eclipse-Plugin-2.7.3,其中包含hadoop-eclipse-plugin-2.7.3.jar和Windows-Eclipse下配置Hadoop Map/Reduce开发环境所需的相关文件。

    hadoop-eclipse-plugin-1.1.2.jar

    Eclipse4.2中配置Map/Reduce视图操作hadoop-1.1.2的插件

    hadoop-eclipse-plugin-2.8.3.jar

    hadoop2.8.3的eclipse插件,eclipse版本是mars,亲测可用。如果遇到这个错误:An internal error occurred during: "Map/Reduce location status updater"不是插件问题,请执行命令:hadoop dfs -mkdir /用户名。

    我使用的HadoopJar.rar

    当前的资源主要用于eclipse中的安装了对应的插件的Hadoop Map/Reduce中的install dictionary中的内容,是Eclipse连接操作hadoop使用的包集合

    hadoop 2.7.6 eclipse插件

    18/05/25 19:51:49 INFO mapreduce.Job: map 0% reduce 0% 18/05/25 19:52:20 INFO mapreduce.Job: map 100% reduce 0% 18/05/25 19:52:29 INFO mapreduce.Job: map 100% reduce 100% 18/05/25 19:52:31 INFO ...

    在Eclipse下的Hadoop0.17.0(MapReduce)的统计作业指导书

    本文用实例讲解了在Eclipse下,使用Hadoop对数据集的统计度量的实现过程。 1.1. 预备条件 熟悉java编程和Eclipse,了解Hadoop,MapReduce,了解linux; 已经建立了Hadoop-0.17.0环境;本文中master的ip为10.63.0.60...

    Hadoop大数据处理实战

    Hadoop的基础内容 一.搭建hadoop分布式环境 二.eclipse/IDEA配置开发环境并实现wordcount 程序 1.演示Hadoop自带的单词统计程序...2.使用eclipse开发工具新建Map/Reduce项目“WordCount“单词统计 3.在虚拟机上部署hive

    自己制作的hadoop2.6.0 eclipse jar包

    自己制作的hadoop2.6.0 eclipse jar包 ...连接hadoop时会出现An internal error occurred during: "Map/Reduce location status updater". java.lang.NullPointerException 不过没关系不会影响文件的增删

Global site tag (gtag.js) - Google Analytics