阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Eclipse配置Hadoop MapReduce开发环境

121次阅读
没有评论

共计 4685 个字符,预计需要花费 12 分钟才能阅读完成。

环境:

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 程序了。

相关阅读

Ubuntu 13.04 上搭建 Hadoop 环境 http://www.linuxidc.com/Linux/2013-06/86106.htm

Ubuntu 12.10 +Hadoop 1.2.1 版本集群配置 http://www.linuxidc.com/Linux/2013-09/90600.htm

Ubuntu 上搭建 Hadoop 环境(单机模式 + 伪分布模式)http://www.linuxidc.com/Linux/2013-01/77681.htm

Ubuntu 下 Hadoop 环境的配置 http://www.linuxidc.com/Linux/2012-11/74539.htm

单机版搭建 Hadoop 环境图文教程详解 http://www.linuxidc.com/Linux/2012-02/53927.htm

搭建 Hadoop 环境(在 Winodws 环境下用虚拟机虚拟两个 Ubuntu 系统进行搭建)http://www.linuxidc.com/Linux/2011-12/48894.htm

更多 Hadoop 相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计4685字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中