window7使用eclipse环境本地运行MapReduce程序方法-----源自网站“神算子”:www.wangsenfeng.com

一、编写目的

    开发的MapReduce在提交到Hadoop集群运行之前,测试是否有bug,希望能在本地使用启动main方法的形式查看是否有错误存在,方便程序的检查和修改。本文档主要针对Windows环境下进行MapReduce开发。

二、环境

    系统:Windows7

    开发环境:eclipse

    Hadoop版本:2.6.0

    准备工作:下载hadoop-2.6.0.tar.gz,解压到磁盘某目录下,然后需要将Hadoop2.6.0加入到环境变量中,设置如下:

说明: C:\Users\kathy\AppData\Local\YNote\data\houbailing@yeah.net\a548bb5f2086461dac256e5a70657e31\clipboard.png

    然后再Path中增加:%HADOOP_HOME%\bin;

三、以WordCount为例详述运行过程及遇到的问题

1、开发WordCount程序

public class WordCount {

 public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{

     private final static IntWritable one = new IntWritable(1);

     private Text word = new Text();

     public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

      StringTokenizer itr = new StringTokenizer(value.toString());

      while (itr.hasMoreTokens()) {

       word.set(itr.nextToken());

       context.write(word, one);

      }

     }

 }

  public static class IntSumReducer extends Reducer<Text,IntWritable,Text,IntWritable> {

   private IntWritable result = new IntWritable();

   public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

    int sum = 0;

    for (IntWritable val : values) {

     sum += val.get();

    }

    result.set(sum);

    context.write(key, result);

   }

  }

  public static void main(String[] args) throws Exception {

   Configuration conf = new Configuration();  // 这里这么设置就可以了

   String[] otherArgs = {"hdfs://imageHandler1:9000/tmp/log/test.log", "hdfs://imageHandler1:9000/tmp/testout111"}; // 可以是hdfs上的路径

   //String[] otherArgs = {"D:/test.log", "D:/test/wordcountout"}; // 也可以是本地路径

   Job job = Job.getInstance(conf, "word count");

   job.setJarByClass(LocalWordCount.class);

   job.setMapperClass(TokenizerMapper.class);

   job.setCombinerClass(IntSumReducer.class);

   job.setReducerClass(IntSumReducer.class);

   job.setOutputKeyClass(Text.class);

   job.setOutputValueClass(IntWritable.class);

   for (int i = 0; i < otherArgs.length - 1; ++i) {

    FileInputFormat.addInputPath(job, new Path(otherArgs[i]));

   }

   FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));

   System.exit(job.waitForCompletion(true) ? 0 : 1);

   }

}

2、运行WordCount

1)此时使用Run as->Java Application运行,会报如下类似错误:

2015-01-22 15:31:47,782 [main] WARN org.apache.hadoop.util.NativeCodeLoader (NativeCodeLoader.java:62) - Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

2015-01-22 15:31:47,793 [main] ERROR org.apache.hadoop.util.Shell (Shell.java:373) - Failed to locate the winutils binary in the hadoop binary path

java.io.IOException: Could not locate executable D:\hbl_study\hadoop2\hadoop-2.6.0\bin\winutils.exe in the Hadoop binaries.

 at org.apache.hadoop.util.Shell.getQualifiedBinPath(Shell.java:355)

 at org.apache.hadoop.util.Shell.getWinUtilsPath(Shell.java:370)

 ......

该错误是找不到winutils.exe,需要将winutils.exe拷贝到hadoop2.6.0/bin目录下,winutils.exe如下:

2)再次运行报错类似:

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

 at org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Native Method)

 at org.apache.hadoop.io.nativeio.NativeIO$Windows.access(NativeIO.java:557)

 at org.apache.hadoop.fs.FileUtil.canRead(FileUtil.java:977)

 ......

该错误是缺少hadoop.dll(hadoop2.6.0编译的版本)文件,需要将hadoop.dll拷贝到hadoop2.6.0/bin目录下,hadoop.dll如下:

再次运行没有报错。

说明:在网上有很多hadoop.dll资源,我开始下载了一个,放入hadoop2.6.0/bin后报错如下:

java.lang.UnsatisfiedLinkError: org.apache.hadoop.util.NativeCrc32.nativeComputeChunkedSumsByteArray(II[BI[BIILjava/lang/String;JZ)V

 at org.apache.hadoop.util.NativeCrc32.nativeComputeChunkedSumsByteArray(Native Method)

 at org.apache.hadoop.util.NativeCrc32.calculateChunkedSumsByteArray(NativeCrc32.java:86)

 at org.apache.hadoop.util.DataChecksum.calculateChunkedSums(DataChecksum.java:430)

 ......

这是由于我下载的hadoop.dllhadoop2.2.0编译成的文件,网上大部分hadoop.dll都是hadoop2.2.0编译而成的,因此在使用hadoop2.6.0运行程序时会报错,推测可能是版本不匹配或者对应的类已经发生了改变,原来版本编译的hadoop.dll已经不适用。因此我自己编译了一个hadoop2.6.0对应的hadoop.dll,问题得到解决。如果以后hadoop继续进行升级,我编译好的hadoop.dll也不再使用,因此下面我分享一下我的编译方法,以供版本变化后可以自己编译该文件。

四、Window7 编译Hadoop2.6.0源码生成hadoop.dll

说明:在Windows7环境中我并没有将源码完全编译成功,只是成功生成了hadoop.dll。我暂没有找到在Windows7下编译全部hadoop源码成功的方法。

1、准备工作:

1)下载hadoop-2.6.0-src.tar.gz

2Microsoft Windows SDK v7.1Visual Studio 2010

3Maven3.0以上,我使用的3.1.1,安装后需要配置环境变量如下

说明: C:\Users\kathy\AppData\Local\YNote\data\houbailing@yeah.net\ba6c9d32b5e74e28992e0ad7bebccd1f\clipboard.png

Path中加入:%maven_home%\bin;

输入mvn -version验证。

4Protocol Buffers 2.5.0,现在已经下载不到,附上该附件如下:

安装方法:解压protobuf-2.5.0.tar.gz到某目录下,例如D:\protobuf-2.5.0,解压protoc-2.5.0-win32.zip获得protoc.exe,将protoc.exe放入D:/protobuf-2.5.0目录下,并在环境变量Path中加入D:\protobuf-2.5.0。打开命令行输入“protoc --version”验证,若显示libprotoc 2.5.0代表安装成功。

5Cygwin

6JDK 1.6+,我使用的是JDK1.7.0_60

7CMake2.6以上,我用的版本是3.1.0cmake-3.1.0-win32-x86.zip

解压后配置环境变量:

说明: C:\Users\kathy\AppData\Local\YNote\data\houbailing@yeah.net\df0aafe4747b4fabb4a4381dcc1bd0de\clipboard.png

Path中加入%CMAKE_HOME%\bin;

8)畅通的网络

2、开始编译

如果使用Microsoft Windows SDK v7.1,需要打开“开始”--“所有程序”--Microsoft Windows SDK v7.1--Windows SDK 7.1 Command Prompt”,进入VC++的命令行工具(一定要从此处进入方可顺利编译Hadoop源代码,记着是以管理员身份运行)。

切换至源代码根目录,执行编译命令:mvn package -Pdist,native-win -DskipTests -Dtar

等待一段时间会有一个类似下面的报错:

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2:exec (compile-ms-winutils) on project hadoop-common: Command execution failed. Process exited with an error: 1(Exit value: 1) -> [Help 1]

[ERROR]

[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.

[ERROR] Re-run Maven using the -X switch to enable full debug logging.

[ERROR]

[ERROR] For more information about the errors and possible solutions, please read the following articles:

[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

[ERROR]

[ERROR] After correcting the problems, you can resume the build with the command

[ERROR]   mvn <goals> -rf :hadoop-common

目前我没有找到好的解决办法,只能修改hadoop2.6.0\hadoop-common-project\hadoop-common目录下的pom.xml文件:搜索“${basedir}/src/main/winutils/winutils.sln”,将这段代码所在的<execution>注释掉。

<!--<execution>

<id>compile-ms-winutils</id>

<phase>compile</phase>

<goals>

<goal>exec</goal>

</goals>

<configuration>

<executable>msbuild</executable>

<arguments>

<argument>${basedir}/src/main/winutils/winutils.sln</argument>

<argument>/nologo</argument>

<argument>/p:Configuration=Release</argument>

<argument>/p:OutDir=${project.build.directory}/bin/</argument>

<argument>/p:IntermediateOutputPath=${project.build.directory}/winutils/</argument>

<argument>/p:WsceConfigDir=${wsce.config.dir}</argument>

<argument>/p:WsceConfigFile=${wsce.config.file}</argument>

</arguments>

</configuration>

</execution>-->

再进行编译,后来会报错,但是此时在hadoop2.6.0\hadoop-common-project\hadoop-common\target\hadoop-common-2.6.0\bin目录下已经生成了hadoop.dll文件,我们的目的达到了。

如果使用VS2010,需要在Path中加入“C:\Windows\Microsoft.NET\Framework64\v4.0.30319”。然后打开命令提示符进入到源码根目录输入编译命令即可。

这两种方式我都亲测过。

以上任何对环境变量的修改,都需要重新启动电脑使配置生效,因此可将所需软件全部安装配置好后再重启电脑。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值