Eclipse配置Hadoop MapReduce开发环境

环境:

Eclipse版本:MyEclipse6.5.1

Hadoop版本:hadoop-1.2.1

 

1.安装MyEclipse后,创建一个java项目

File->New->Java Project

输入项目名称,确定

Eclipse配置Hadoop MapReduce开发环境

 

2.导入hadoop所有包

解压hadoop-1.2.1.tarE:\software\share\hadoop-1.2.1

E:\software\share\hadoop-1.2.1

E:\software\share\hadoop-1.2.1\lib下的jar包都导入到项目里

方法如下:

点中项目根右键->Properties->JavaPath->Libraries->Add External JARs

Eclipse配置Hadoop MapReduce开发环境

 

3.确认jre6.0以上版本

我的MyEclipse6.5.1版本开始默认使用jre5.0版本,因hadoop-1.2.1需要jre 6.0以上版本,所执行程序时报错:

Bad version number in .class file (unableto load class ***)

 

更改jre版本方法

Windows->Preference->Java->InstalledJREsàadd

Eclipse配置Hadoop MapReduce开发环境

 

4.修改FileUtil.java文件

这时在创建一个测试WordCountmapreduce程序时,同样遇到了下面的问题

 

13/12/13 22:58:49 WARNutil.NativeCodeLoader: Unable to load native-hadoop library for yourplatform... using builtin-java classes where applicable

13/12/13 22:58:49 ERRORsecurity.UserGroupInformation:PriviledgedActionExceptionas:liczcause:java.io.IOException: Failed to set permissions of path:\tmp\hadoop-licz\mapred\staging\licz1853164772\.staging to 0700

Exception in thread"main"java.io.IOException: Failed to set permissions of path:\tmp\hadoop-licz\mapred\staging\licz1853164772\.staging to

......

 

解决办法:

修改E:\software\share\hadoop-1.2.1\src\core\org\apache\hadoop\fs\FileUtil.java文件

注释掉下面的内容

 

685 private static voidcheckReturnValue(boolean rv, File p,

686 FsPermission permission

687 ) throws IOException {

688 /*if (!rv) {

689 throw new IOException("Failed toset permissions of path: " + p +

690 " to " +

691 String.format("%04o", permission.toShort()));

692 }*/

693 }

 

然后在Mapreduce1/scr新建一个org.apache.hadoop.fs包,把FileUtil.java文件拷到这个包的下面(在eclipse里直接粘贴就可以)

Eclipse配置Hadoop MapReduce开发环境

 

再次编译WordCount.java程序没有报错

import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
importorg.apache.hadoop.mapred.FileOutputFormat;
importorg.apache.hadoop.mapred.JobClient;
importorg.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
importorg.apache.hadoop.mapred.Mapper;
importorg.apache.hadoop.mapred.OutputCollector;
importorg.apache.hadoop.mapred.Reducer;
importorg.apache.hadoop.mapred.Reporter;
importorg.apache.hadoop.mapred.TextInputFormat;
importorg.apache.hadoop.mapred.TextOutputFormat;

public class WordCount {

    public static class WordCountMapper extends MapReduceBase implementsMapper<Object, Text, Text, IntWritable> {
        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();


        public void map(Object key, Text value,OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
            StringTokenizer itr = newStringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                output.collect(word, one);
            }

        }
    }

    public static class WordCountReducer extends MapReduceBase implementsReducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();


        public void reduce(Text key, Iterator<IntWritable>values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
            int sum = 0;
            while (values.hasNext()) {
                sum +=values.next().get();
            }
            result.set(sum);
            output.collect(key, result);
        }

    }

    public static void main(String[] args) throws Exception {
        String input = "hdfs://192.168.2.100:9000/user/licz/hdfs/o_t_account";
        String output = "hdfs://192.168.2.100:9000/user/licz/hdfs/o_t_account/result";

        JobConf conf = new JobConf(WordCount.class);
        conf.setJobName("WordCount");
        conf.addResource("classpath:/hadoop/core-site.xml");
        conf.addResource("classpath:/hadoop/hdfs-site.xml");
        conf.addResource("classpath:/hadoop/mapred-site.xml");

        conf.setOutputKeyClass(Text.class);
        conf.setOutputValueClass(IntWritable.class);

      conf.setMapperClass(WordCountMapper.class);
      conf.setCombinerClass(WordCountReducer.class);
      conf.setReducerClass(WordCountReducer.class);

      conf.setInputFormat(TextInputFormat.class);
        conf.setOutputFormat(TextOutputFormat.class);

        FileInputFormat.setInputPaths(conf, new Path(input));
        FileOutputFormat.setOutputPath(conf,new Path(output));

        JobClient.runJob(conf);
        System.exit(0);
    }

}

Eclipse配置Hadoop MapReduce开发环境

注意:

在windows上使用eclipse用户要与hadoop服务器上安装hadoop的用户名一致,这样才能正常运行,否则会出现没有权限创建目录的报错。

如hadoop安装在了linux服务器的licz用户下,我必需在windows的上的licz用户下使用eclipse开发程序。

这样,我们就可以在eclipse上开发mapreduce程序了。

相关阅读

相关推荐