Windowsdows 8.0上Eclipse 4.4.0 配置CentOS 6.5 上的Hadoop2.2.0开发环境

图文详解如何在windows 8.0上的eclipse 4.4.0中配置centos 6.5上的hadoop 2.2.0开发环境,供有需要的朋友参考学习。

Eclipse的Hadoop插件下载地址:https://www.php.cn/link/bf74cbe3722200f6fad86af0b239d900

下载压缩包后,解压并将hadoop-eclipse-kepler-plugin-2.2.0.jar文件放置到Eclipse的dropins目录中,重启Eclipse即可。

进入Windows -> Preferences配置根目录。

,这里的hadoop installation directory不是指你在Windows上安装的Hadoop目录,而是你在CentOS上编译好的源码在Windows上的解压路径。这个路径仅用于创建MapReduce项目时自动引入所需的jar文件。

进入Window -> Open Perspective -> Other -> Map/Reduce,打开Map/Reduce窗口。

找到

,右击选择New Hadoop location,这时会出现

Map/Reduce(V2)中的配置对应于mapred-site.xml中的端口配置,DFS Master中的配置对应于core-site.xml中的端口配置,配置完成后点击finish即可。这时可以查看

进行测试,创建一个新的MapReduce项目,

,要解决这个问题,你需要在Windows上配置HADOOP_HOME,并将%HADOOP_HOME%\bin添加到path中。然后从https://github.com/srccodes/hadoop-common-2.2.0-bin下载文件,下载后将bin目录中的内容全部复制到你自己在Windows上的Hadoop bin目录中,覆盖即可。同时,将hadoop.dll添加到C盘的system32目录中。如果完成这些步骤后仍然遇到“Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z”,请检查你的JDK版本,可能是由于使用了32位JDK导致的,需要下载并安装64位JDK,并在Eclipse中将JRE环境配置为新安装的64位JRE。

。比如我的jre1.8是64位,而jre7是32位,如果列表中没有你需要的JRE,直接点击add添加即可,选择你的64位JRE环境后,问题就会解决。

接下来编写一个wordcount程序进行测试,下面是我的代码,前提是你已经在HDFS上创建了input文件,并在其中放置了一些内容:

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 {
        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 {
        private IntWritable result = new IntWritable();

        public void reduce(Text key, Iterable 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 {
        // System.setProperty("hadoop.home.dir", "E:\\\\hadoop2.2\\\\");
        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 = new Job(conf, "word count");
        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("hdfs://master:9000/input"));
        FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/output"));
        boolean flag = job.waitForCompletion(true);
        System.out.print("SUCCEED!" + flag);
        System.exit(flag ? 0 : 1);
        System.out.println();
    }
}