window上eclipse调试基于hadoop2.7.3的MapReduce程序

1,环境配置:

配置系统环境变量HADOOP_HOME,指向hadoop安装目录(如果你不想招惹不必要的麻烦,不要在目录中包含空格或者中文字符),把HADOOP_HOME/bin加到PATH环境变量(非必要,只是为了方便)。点击http://download.csdn.net/detail/wuxun1997/9841472或https://pan.baidu.com/s/1o78V1hS下载相关工具类,直接解压后把文件丢到D:\hadoop-2.7.3\bin目录中去,将其中的hadoop.dll在c:/windows/System32下也丢一份。

2,建立maven项目:

pom.xml配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com</groupId>
  <artifactId>mr01</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>  
        <dependency>  
            <groupId>org.apache.hadoop</groupId>  
            <artifactId>hadoop-common</artifactId>  
            <version>2.7.3</version>  
        </dependency>  
        <dependency>  
            <groupId>org.apache.hadoop</groupId>  
            <artifactId>hadoop-hdfs</artifactId>  
            <version>2.7.3</version>  
        </dependency>  
        <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>
    
</project>

3,源代码:

代码WordMapper:

复制代码
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
 
public class WordMapper extends Mapper<LongWritable,Text, Text, IntWritable> {
 
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
            throws IOException, InterruptedException {
        String line = value.toString();
        String[] words = line.split(" ");
        for(String word : words) {
            context.write(new Text(word), new IntWritable(1));
        }
    }
     
}
复制代码

代码WordReducer:

复制代码
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
 
public class WordReducer extends Reducer<Text, IntWritable, Text, LongWritable> {
 
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values,
            Reducer<Text, IntWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        long count = 0;
        for(IntWritable v : values) {
            count += v.get();
        }
        context.write(key, new LongWritable(count));
    }
     
}
复制代码

代码Test:

复制代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
 
 
public class Test {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
                         
        Job job = Job.getInstance(conf);
         
        job.setMapperClass(WordMapper.class);
        job.setReducerClass(WordReducer.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
         
        FileInputFormat.setInputPaths(job, "D:/hadoop_test/test_data/words.txt");
        FileOutputFormat.setOutputPath(job, new Path("D:/hadoop_test/output/201711290952"));
         
        job.waitForCompletion(true);
    }
}
复制代码

把hdfs中的文件拉到本地来运行

FileInputFormat.setInputPaths(job, "hdfs://master:9000/wcinput/");
FileOutputFormat.setOutputPath(job, new Path("hdfs://master:9000/wcoutput2/"));

注意这里是把hdfs文件拉到本地来运行,如果观察输出的话会观察到jobID带有local字样
同时这样的运行方式是不需要yarn的(自己停掉yarn服务做实验)
在远程服务器执行

复制代码
conf.set("fs.defaultFS", "hdfs://master:9000/");
 
conf.set("mapreduce.job.jar", "target/wc.jar");
conf.set("mapreduce.framework.name", "yarn");
conf.set("yarn.resourcemanager.hostname", "master");
conf.set("mapreduce.app-submission.cross-platform", "true");

FileInputFormat.setInputPaths(job, "/wcinput/");
FileOutputFormat.setOutputPath(job, new Path("/wcoutput3/"));
复制代码

如果遇到权限问题,配置执行时的虚拟机参数-DHADOOP_USER_NAME=root
也可以将hadoop的四个配置文件拿下来放到src根目录下,就不需要进行手工配置了,默认到classpath目录寻找
或者将配置文件放到别的地方,使用conf.addResource(.class.getClassLoader.getResourceAsStream)方式添加,不推荐使用绝对路径的方式


猜你喜欢

转载自blog.csdn.net/u012592062/article/details/78662634