hadoop实践(五)win10+eclipse+hadoop2.6.4 开发环境

来源:互联网 发布:mac修照片的软件 编辑:程序博客网 时间:2024/06/06 04:09

本人环境为win10(才从win7升级)

        eclipse是  Kepler Service Release 1

        在win10下,安装了vm workstation,其上安装了centos6.5 ,并在上面部署了hadoop2.6.4的伪分布式安装

一、 目标

        设置win10下的eclipse开发环境,并且可以在此环境上开发hadoop应用,并在伪分布式hadoop环境下测试。  

二、准备

       1、eclipse  (  Kepler Service Release 1)

       2、 hadoop2.6.4

       3、 hadoop.dll 和  winutils

       4、 wordcount 代码

      5、 wordcount 所需要的统计单词的文本源

      6、 hadoop for eclipse的插件,本人使用的插件为 hadoop-eclipse-plugin-2.6.4.jar

       

三、环境搭建步骤

      1、 将hadoop2.6.4解压 在win10系统的任意目录下。 (就是为了配置eclipse用,实际联调的时候,是连接linux 虚机上的伪分布式hadoop)

      2、 设置win10的环境变量,通过控制面板-》系统-》高级设置-》环境变量  需要设置如下几个环境变量,已本人机器为例:

           

                JAVA_HOME=C:\Program Files (x86)\Java\jre6\bin               

                HADOOP_HOME=E:\cwqwork\develop\hadoop-2.6.4

                path 增加最后 E:\cwqwork\develop\hadoop-2.6.4\bin

       3、拷贝插件到  eclipse安装目录下的plugsin目录

       4、 启动eclipse,  windows-》hadoop Map/Reduce

               在 hadoop installation directory 里面,填入 前面第1步解压的目录,点击OK

        5、 界面最右边新出先的 Map/Reduce标签点中, 在最左边Project Explorer 会出现  DFS Locations。

              界面最右下角有个蓝色小象,点击后,设置  hadoop location

         6、上面设置好后,就可以 一层一层浏览   DFS Locations。 这里显示的是 linux下hadoop的dfs系统

四、  测试工程代码

          1、 新建工程,选other -》map reduce project, 然后输入工程名称等等,建立新的工程

          2、 创建 WordCount 类

                 代码如下:

import java.io.IOException;import java.util.StringTokenizer;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IntWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Job;import org.apache.hadoop.mapreduce.Mapper;import org.apache.hadoop.mapreduce.Reducer;import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import org.apache.hadoop.util.GenericOptionsParser;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();    //conf.set("mapred.job.tracker","192.168.136.155:9001" );    //conf.set("fs.default.name","192.168.136.155:9000" );        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();       if (otherArgs.length != 2) {      System.err.println("Usage: wordcount <in> <out>");      System.exit(2);    }    System.out.println ("Usage: wordcount <in> <out>" + otherArgs[0] +"  "+ otherArgs[1] );        Job job = new Job(conf, "wordcount");    job.setJarByClass(WordCount.class);    job.setMapperClass(TokenizerMapper.class);    job.setCombinerClass(IntSumReducer.class);    job.setReducerClass(IntSumReducer.class);    job.setOutputKeyClass(Text.class);    job.setOutputValueClass(IntWritable.class);    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));        System.out.println ("add input path:" + otherArgs[0]);        System.out.println ("set output path:" + otherArgs[1]);     System.out.println ("begin wait job completion");        job.waitForCompletion(true);    }}


                 创建完成后, 在linux 虚机上导入需要统计的文本

                 文本1:Hello world Hello me! cwq solo

                 文本2: Hello world Hello you! solo

                 在linux 目录 /opt/hadoop/input/wordcount  下:

                      echo "Hello world Hello me! cwq solo"  >test1.txt

                      echo " Hello world Hello you! solo"  >test2.txt

             hadoop fs -put  /opt/hadoop/input/wordcount input

       3、 完成后,在类上右键-》run configuration-》 输入参数

                 hdfs://192.168.136.155:9000/user/hadoop/input/wordcount  hdfs://192.168.136.155:9000/user/hadoop/output/wordcount

                 输入后,不要执行。

                然后,用run on hadoop 方式执行。


          4、 正常情况下,会报异常:

Exception in thread "main" java.lang.NullPointerExceptionat java.lang.ProcessBuilder.start(ProcessBuilder.java:441)at org.apache.hadoop.util.Shell.runCommand(Shell.java:445)at org.apache.hadoop.util.Shell.run(Shell.java:418)

             原因是,没有安装补丁。  将 hadoop.dll 和  winutils 拷贝到  win10上hadoop目录下bin目录。

             


          5、 再次运行,没有异常,但是运行结束,查看dfs 没有output结果, console没有输出异常。 这里纠结很久。

                 解决办法:在src 目录下,建立log.properities文件,使得log4j 可以打印

            

                 log4j.rootLogger=debug,stdout,R                 log4j.appender.stdout=org.apache.log4j.ConsoleAppender                 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout                 log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n                 log4j.appender.R=org.apache.log4j.RollingFileAppender                 log4j.appender.R.File=mapreduce_test.log                 log4j.appender.R.MaxFileSize=1MB                 log4j.appender.R.MaxBackupIndex=1                 log4j.appender.R.layout=org.apache.log4j.PatternLayout                 log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%                 log4j.logger.com.codefutures=DEBUG

              6、再次运行,console打印会有error

 WARN - job_local194089354_0001
org.apache.hadoop.security.AccessControlException: Permission denied: user=Administrator, access=WRITE, inode="/user/hadoop/output":hadoop:supergroup:drwxr-xr-x
at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.checkFsPermission(FSPermissionChecker.java:271)


                说明是权限问题, eclipse是用Administrator启动的,连接linux下的hadoop是用此用户,所以权限禁止。

解决办法:

1)、如果是测试环境,可以取消hadoop hdfs的用户权限检查。打开conf/hdfs-site.xml,找到dfs.permissions属性修改为false(默认为true)OK了。
2)、修改hadoop location参数,在advanced parameter选项卡中,找到hadoop.job.ugi项,将此项改为启动hadoop的用户名即可
3)、 修改window 机器的用户名为 hadoop 用户名。

 

          7、执行,这次正确执行完成,console 不报告错误, dfs location 右键 -》reconnect -》一层一层点开,最后output 目录下看到统计单词结果。

Hello 4
cwq 1
me! 1
solo 2
world 2
you! 1





                

0 0
原创粉丝点击