Windows10下Eclipse搭建Hadoop3开发环境

前言

由于笔记本配置限制,虚拟机CentOs-7关闭了图形界面,作者在Windows端编写mapreduce程序然后在linux上运行。

工具

windows 10
CentOs-7(已安装,见上一博文)
eclipse-jee-oxygen-2-win32-x86_64.zip(采用其他精简版的eclipse可能会出问题)
jdk1.8(请保证jdk位数跟计算机位数一致)

在Windows上解压Hadoop3

  • 我们编程要用到hadoop的库,需要将hadoop-3.0.0.tar.gz(最好跟linux上hadoop版本一致)解压一下,然后前往https://download.csdn.net/download/junior19/10292556下载这个东西,覆盖掉hadoop-3.0.0\bin文件夹;接着将bin里面的hadoop.dll复制到C:\Windows\System32中。(hadoop.dll文件尽量用最新版的)

  • 到系统->高级系统设置->环境变量下面的系统变量处选择“新建”
    Windows10下Eclipse搭建Hadoop3开发环境_第1张图片
    然后设置一下PATH
    Windows10下Eclipse搭建Hadoop3开发环境_第2张图片

开放Hadoop的权限

  • 为了能在Ecplise上对Linux的HDFS文件操作,需要设置一下权限。
  • 进入Linux修改里面的hdfs-site.xml,添加下面代码
<property>
   <name>dfs.permissionsname>
   <value>falsevalue>
property>
  • 请保证已经在hadoop上已经创建了用户以及新建了input文件夹,如果之前没做这一步请执行以下命令(开启Hadoop集群后)
hadoop dfs -mkdir -p /user/hadoop
hadoop dfs -mkdir input
  • (开启Hadoop集群后)执行hadoop fs -chmod 777 /user/hadoop

在Eclipse上安装Hadoop插件

  • 自行去下载hadoop-eclipse-plugin-2.6.0.jar,放到Eclipse的plugins目录下,重启Eclipse。

  • 打开eclipse,在window->Preferences->Hadoop Map/Reduce下设置Hadoop的解压路径
    Windows10下Eclipse搭建Hadoop3开发环境_第3张图片

  • 点击window->show view->other->map/reduce locations OPEN。

  • 也可以在右上角的这里切换到map/reduce项目(以后可以在这里切回去Resource界面)
    Windows10下Eclipse搭建Hadoop3开发环境_第4张图片
  • 右键new一个
    Windows10下Eclipse搭建Hadoop3开发环境_第5张图片
  • 配置如下,Host那里最好直接填IP地址,如果像我这样填Linux主机名请先在Windows的Hosts文件设置好IP映射
    Windows10下Eclipse搭建Hadoop3开发环境_第6张图片
  • 然后点击右边设置一下hadoop.tmp.dir的地址,跟core-site.xml的要一致
    Windows10下Eclipse搭建Hadoop3开发环境_第7张图片

  • 还有这个我们之前设置为1
    这里写图片描述

  • Finish后可能会报NullPointer错误,貌似没影响先不理它。

运行WordCount例子

  • 先去linux启动下Hadoop集群start-all.sh
  • 此时Eclipse应该能看到以下内容,没有就试试右键刷新下
    Windows10下Eclipse搭建Hadoop3开发环境_第8张图片
  • 注意要先创建一些文件到input文件夹内,创建方法可以在linux上用命令行上传上去,也可以先在windows新建好一些形如input1.txt文件,里面随便填一些句子hello word之类的,然后在Eclipse直接上传上去。
    Windows10下Eclipse搭建Hadoop3开发环境_第9张图片
  • 新建项目,File->new->Project->Map/Reduce project ,包名最好留空!否则最后运行老会遇到”找不到class”错误。
  • 新建一个class命名为WordCount加入下面代码
import java.io.IOException;
import java.util.Iterator;
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 WordCount() {
    }
     public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();
        if(otherArgs.length < 2) {
            System.err.println("Usage: wordcount  [...] ");
            System.exit(2);
        }
        Job job = Job.getInstance(conf, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(WordCount.TokenizerMapper.class);
        job.setCombinerClass(WordCount.IntSumReducer.class);
        job.setReducerClass(WordCount.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);
    }
    public static class TokenizerMapper extends Mapper {
        private static final IntWritable one = new IntWritable(1);
        private Text word = new Text();
        public TokenizerMapper() {
        }
        public void map(Object key, Text value, Mapper.Context context) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString()); 
            while(itr.hasMoreTokens()) {
                this.word.set(itr.nextToken());
                context.write(this.word, one);
            }
        }
    }
public static class IntSumReducer extends Reducer {
        private IntWritable result = new IntWritable();
        public IntSumReducer() {
        }
        public void reduce(Text key, Iterable values, Reducer.Context context) throws IOException, InterruptedException {
            int sum = 0;
            IntWritable val;
            for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {
                val = (IntWritable)i$.next();
            }
            this.result.set(sum);
            context.write(key, this.result);
        }
    }
}
  • 右键class选择run as->Run configuations设置如下,当然里面的IP填你Linux主机的IP,然后RUN即可。
    Windows10下Eclipse搭建Hadoop3开发环境_第10张图片
  • 输出结果如下,下次执行的话需要先将output文件夹删除掉。
  • Windows10下Eclipse搭建Hadoop3开发环境_第11张图片

打包JAR在linux运行

  • 如果上述步骤搞不好,无法在eclipse直接运行代码,也可以export出一个jar包,通过SFTP发到linux上执行
hadoop jar WordCount.jar WordCount input output

这里中间的WordCount貌似是填Main函数的所在Class名,但是网上说填包名,我报错无数次(找不到class)之后,在新建project时不用包名才成功运行。

你可能感兴趣的:(Hadoop)