zoukankan      html  css  js  c++  java
  • myeclipse下搭建hadoop2.7.3开发环境

    需要下载的文件:链接:http://pan.baidu.com/s/1i5yRyuh 密码:ms91

    一  下载并编译  hadoop-eclipse-plugin-2.7.3.jar

    二  将hadoop-eclipse-plugin-2.7.3.jar放到myeclipse的安装目录下的plugins目录下,并重启myeclipse

      在windows->preferences下可看见hadoop Map/Reduce界面,路径选择你WINDOWS下的hadoop解压后的路径。

    三 选择Windows->show view->others下的MapReduce Locations

    四  新建一个配置 配置如下

    host为你的远程hadoop待连接的主机IP地址

    Port:50030 对应mapred-site.xml下的jobtracher地址,如下

     

    Port:9000对应core-site.xml下的fs.default.name的端口

    user name 填你windows的用户名; 

    修改Advanced parameters下的参数

    值对应 core-site.xml下的hadoop.tmp.dir参数

    修改hdfs-site.xml下的dfs.permissions参数,允许连接

    四 保存配置参数并重启myeclipse,可以看见如下的文件结构说明配置连接成功。

    五 下载hadoop.ll和winutils.exe 到windows的hadoop/bin目录下

    并将hadoop.dll添加到windows->system32目录下

    五 环境测试

      新建项目:File-->New-->Other-->Map/Reduce Project ,项目名可以随便取

      它会自动添加依赖包,如下:

       

     新建如下文件:

    编写实现代码,与官方例子为例

    package com.duking.hadoop;

    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<Object, Text, Text, IntWritable>{

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

    public void reduce(Text key, Iterable<IntWritable> 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 {
    Configuration conf = new Configuration();

    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length != 2) {
    System.err.println(otherArgs.length);
    System.err.println("Usage: wordcount <in> <out>");
    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(otherArgs[0]));
    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
    }

    右击wordcount,选择run as - run configurations

    右击wordcount-run as -run on hadoop

    注意:HDFS的目录结构应如下:

    protocols为输入待计算的数据。

    查看运行结果

    至此环境搭建成功!!!!!!!!!!

    问题总结:环境搭建好后运行mapreduce程序发现output目录下为空,但把程序打包为jar到hadoop环境下运行是有数据输出的。

    最后查资料解决方法如下:首先把

    这个文件加入工程目录,注意解压的hadoop目录下有两个这个文件,不要加错了。

    最后工程目录如下

    然后运行程序发现报错了,错误提示为:Could not locate executable null

    查阅资料后发现是没有添加HADOOP_HOME环境变量,添加即可。

    如果不想重启电脑可以在代码下加如下代码

    注意路径改为自己的windows  hadoop路径

  • 相关阅读:
    小白必读:闲话HTTP短连接中的Session和Token
    网络编程懒人入门(六):深入浅出,全面理解HTTP协议
    IM系统的MQ消息中间件选型:Kafka还是RabbitMQ?
    致我们再也回不去的 Github ...
    了不起的WebRTC:生态日趋完善,或将实时音视频技术白菜化
    网络编程懒人入门(六):史上最通俗的集线器、交换机、路由器功能原理入门
    盘点微信的前世今生,微信成功的必然和偶然
    微信七年回顾:历经多少质疑和差评,才配拥有今天的强大
    写给小白的实时音视频技术入门提纲
    jenkins使用jacoco插件检测代码覆盖率(八)
  • 原文地址:https://www.cnblogs.com/duking1991/p/6056923.html
Copyright © 2011-2022 走看看