Eclipse上搭建Hadoop开发环境

一、概述
1.实验使用的Hadoop集群为伪分布式模式,Eclipse相关配置已完成;
2.软件版本为hadoop-2.7.3.tar.gz、apache-maven-3.5.0.rar。
 
二、使用eclipse连接hadoop集群进行开发
1.在开发主机上配置hadoop
①将hadoop-2.7.3.tar.gz解压到本地主机上

Eclipse上搭建Hadoop开发环境

②使用windows版本的hadoop中的bin替换目标中的bin文件夹

Eclipse上搭建Hadoop开发环境

③配置windows上的hadoop环境变量

2.在eclipse上配置hadoop集群信息
①在eclipse中添加hadoop路径

Eclipse上搭建Hadoop开发环境

②配置hadoop集群访问信息

Eclipse上搭建Hadoop开发环境

3.在hadoop集群中取消权限验证

hdfs-site.xml
<property>
    <name>dfs.permissions</name>
    <value>false</value>
</property>

4.创建一个文件测试连接权限

5.安装maven
①将maven解压到开发主机上

②在eclipse上添加maven路径

Eclipse上搭建Hadoop开发环境

5.新建maven工程

6.修改maven配置文件(maven/pom.xml)
  <dependencies>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.7.3</version>
    </dependency>
    <dependency> 
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
    </dependency>
  </dependencies>

7.新建一个类用于测试(WordCount)

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("Usage: wordcount <in> [<in>...] <out>");
      System.exit(2);
    }
    Job job = Job.getInstance(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);
    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);
  }
}

8.配置WordCount
①将log4j.properties移动到WordCount类下
②设置WordCount的运行自变量

Eclipse上搭建Hadoop开发环境


 
8.运行测试

Eclipse上搭建Hadoop开发环境


 
三、jar包的导出与提交执行
1.导出WordCount

2.将导出的jar包上传到hadoop集群

[hadoop@hadoop ~]$ ls
wc.jar

3.运行

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/60ea6453d0dbfd4777a2c438c6fd70a6.html