欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个简单的MR程序

程序员文章站 2024-01-31 13:37:58
...

一、pom.xml

    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>2.5.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.5.0</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-hdfs</artifactId>
      <version>2.5.0</version>
    </dependency>

二、hadoop环境信息加入resources内

一个简单的MR程序

三、MR wordCount示例

package com.cys.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.net.URI;

public class WordCountMapReducer extends Configured implements Tool {

	//Map类实现map方法,类型为map键值得输入输出类型<LongWritable, Text, Text, LongWritable>
    public static class WCMapper extends Mapper<LongWritable, Text, Text, LongWritable> {

		
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String line = value.toString();
            String[] words = line.split(" ");
            for(String word: words){

				//将map输出提交给context,context对象衔接map和reduce
                context.write(new Text(word),new LongWritable(1));
            }

        }
    }

    public static class WCReducer extends Reducer<Text, LongWritable, Text, LongWritable> {

        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            long sum = 0;
            for(LongWritable value: values){
                sum += value.get();
            }

            System.out.println("key: "+key+",value: "+sum);
            context.write(key, new LongWritable(sum));
        }
    }

    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = new Configuration();
        //conf.addResource("core-site.xml");
        conf.set("mapreduce.app-submission.cross-platform","true");
        conf.set("fs.defaultFS", "hdfs://bigdata01:8020");// 指定namenode
        conf.set("mapreduce.framework.name", "yarn"); // 指定使用yarn框架
        conf.set("yarn.resourcemanager.address", "bigdata01:8032"); // 指定resourcemanager
        conf.set("yarn.resourcemanager.scheduler.address", "bigdata01:8030");// 指定资源分配器
        conf.set("mapreduce.jobhistory.address", "bigdata01:10020");


        Job job = Job.getInstance(conf,this.getClass().getSimpleName());


        Path pathIn = new Path(args[0]);
        Path pathOut = new Path(args[1]);
        FileSystem fs = FileSystem.get(URI.create(args[1]),conf);

        if(fs.exists(pathOut)){
            fs.delete(pathOut,true);
        }

        FileInputFormat.setInputPaths(job, pathIn);
        FileOutputFormat.setOutputPath(job, pathOut);

        job.setMapperClass(WCMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setReducerClass(WCReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        job.setJarByClass(WordCountMapReducer.class);
        //job.setJar("C:\\Users\\cys\\IdeaProjects\\MRTest\\out\\artifacts\\MRTest_jar\\MRTest.jar");


        boolean isSuccess = job.waitForCompletion(true);
        return isSuccess?1:-1;
    }

    public static void main(String[] args) throws Exception {
        args = new String[]{
                "/input/wordcount.txt",
                "/output"
        };

        ToolRunner.run(new WordCountMapReducer(), args);
    }
}

四、导出jar包提交到hadoop运行

$ bin/yarn jar mrtest.jar
相关标签: HDFS&MR&YARN