win10+eclipse+hadoop2.6.0 开发环境

本人环境为win10

        eclipse是 luna

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

一、 目标

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

二、准备

       1、eclipse  (  Kepler Service Release 1)

       2、 hadoop2.6.0

       3、 hadoop.dll 和  winutils

       4、 wordcount 代码

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

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

       

三、环境搭建步骤

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

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

           

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

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

                path 增加最后 E:\cwqwork\develop\hadoop-2.6.0\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 类

                 代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import java.io.IOException;  
  2. import java.util.StringTokenizer;  
  3.   
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.fs.Path;  
  6. import org.apache.hadoop.io.IntWritable;  
  7. import org.apache.hadoop.io.Text;  
  8. import org.apache.hadoop.mapreduce.Job;  
  9. import org.apache.hadoop.mapreduce.Mapper;  
  10. import org.apache.hadoop.mapreduce.Reducer;  
  11. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  13. import org.apache.hadoop.util.GenericOptionsParser;  
  14.   
  15. public class WordCount {  
  16.   
  17.   public static class TokenizerMapper   
  18.        extends Mapper<Object, Text, Text, IntWritable>{  
  19.   
  20.     private final static IntWritable one = new IntWritable(1);  
  21.     private Text word = new Text();  
  22.   
  23.     public void map(Object key, Text value, Context context  
  24.                     ) throws IOException, InterruptedException {  
  25.       StringTokenizer itr = new StringTokenizer(value.toString());  
  26.       while (itr.hasMoreTokens()) {  
  27.         word.set(itr.nextToken());  
  28.         context.write(word, one);  
  29.       }  
  30.     }  
  31.   }  
  32.   
  33.   public static class IntSumReducer   
  34.        extends Reducer<Text,IntWritable,Text,IntWritable> {  
  35.     private IntWritable result = new IntWritable();  
  36.   
  37.     public void reduce(Text key, Iterable<IntWritable> values,   
  38.                        Context context  
  39.                        ) throws IOException, InterruptedException {  
  40.       int sum = 0;  
  41.       for (IntWritable val : values) {  
  42.         sum += val.get();  
  43.       }  
  44.       result.set(sum);  
  45.       context.write(key, result);  
  46.     }  
  47.   }  
  48.   
  49.   public static void main(String[] args) throws Exception {  
  50.     Configuration conf = new Configuration();  
  51.     //conf.set("mapred.job.tracker","192.168.136.155:9001" );  
  52.     //conf.set("fs.default.name","192.168.136.155:9000" );  
  53.       
  54.     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();  
  55.      
  56.     if (otherArgs.length != 2) {  
  57.       System.err.println("Usage: wordcount <in> <out>");  
  58.       System.exit(2);  
  59.     }  
  60.     System.out.println ("Usage: wordcount <in> <out>" + otherArgs[0] +"  "+ otherArgs[1] );  
  61.       
  62.     Job job = new Job(conf, "wordcount");  
  63.     job.setJarByClass(WordCount.class);  
  64.     job.setMapperClass(TokenizerMapper.class);  
  65.     job.setCombinerClass(IntSumReducer.class);  
  66.     job.setReducerClass(IntSumReducer.class);  
  67.     job.setOutputKeyClass(Text.class);  
  68.     job.setOutputValueClass(IntWritable.class);  
  69.     FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  70.     FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  71.       
  72.     System.out.println ("add input path:" + otherArgs[0]);      
  73.     System.out.println ("set output path:" + otherArgs[1]);   
  74.     System.out.println ("begin wait job completion");  
  75.       
  76.     job.waitForCompletion(true);    
  77.   }  
  78. }  


                 创建完成后, 在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、 正常情况下,会报异常:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Exception in thread "main" java.lang.NullPointerException  
  2. at java.lang.ProcessBuilder.start(ProcessBuilder.java:441)  
  3. at org.apache.hadoop.util.Shell.runCommand(Shell.java:445)  
  4. at org.apache.hadoop.util.Shell.run(Shell.java:418)  

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

             


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

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

            

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. log4j.rootLogger=debug,stdout,R  
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
  3. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
  4. log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n  
  5. log4j.appender.R=org.apache.log4j.RollingFileAppender  
  6. log4j.appender.R.File=mapreduce_test.log  
  7. log4j.appender.R.MaxFileSize=1MB  
  8. log4j.appender.R.MaxBackupIndex=1  
  9. log4j.appender.R.layout=org.apache.log4j.PatternLayout  
  10. log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%  
  11. 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、执行,这次又报错了:java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0

这个时候我们需要在debug的时候环境变量加上HADOOP_HOME=D:\tools\hadoop-2.6.0和PATH=%PATH%;D:\tools\hadoop-2.6.0\bin

如图:


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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值