Hadoop之MapReduce框架的处理过程以及实现集群运行的三种方式

本文介绍了Hadoop MapReduce框架如何简化Key-Value任务的分布式运行,通过词频统计示例解释Map和Reduce阶段的工作原理,并详细阐述在集群上运行MapReduce作业的三种方式:Linux提交至Yarn、本地模拟运行和Eclipse直接运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在没有使用MapReduce框架之前,想要在集群上分布式运行Key-Value等任务的时候,代码特别冗余,会进行不断地连接、在HDFS上读取数据,特别不方便,所以MapReduce框架就将这些准备工作替我们做了

2.结合经典的词频统计代码说一下自己理解的Map(看详细注释)
Map就是由框架读出来数据,根据你自己的需求去处理数据,不用管有几个虚拟机给你运行等一系列不必要的代码,只需要简单的配置一下,直接写出你的核心代码就OK

// KEYIN :是map task读取到的数据的key的类型,是一行的起始偏移量Long
 * VALUEIN:是map task读取到的数据的value的类型,是一行的内容String
 * 
 * KEYOUT:是用户的自定义map方法要返回的结果kv数据的key的类型,在wordcount逻辑中,我们需要返回的是单词String
 * VALUEOUT:是用户的自定义map方法要返回的结果kv数据的value的类型,在wordcount逻辑中,我们需要返回的是整数Integer
 * 
 * 
 * 但是,在mapreduce中,map产生的数据需要传输给reduce,需要进行序列化和反序列化,而jdk中的原生序列化机制产生的数据量比较冗余,就会导致数据在mapreduce运行过程中传输效率低下
 * 所以,hadoop专门设计了自己的序列化机制,那么,mapreduce中传输的数据类型就必须实现hadoop自己的序列化接口
 * 
 * hadoop为jdk中的常用基本类型Long String Integer Float等数据类型封住了自己的实现了hadoop序列化接口的类型:LongWritable,Text,IntWritable,FloatWritable
 * 
 * 
public class WordcountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {

        String string = value.toString();
        String[] words = string.split(" ");//value是读的每一行的数据,利用数据去处理自己想要的结果,我这个呢就是分离出来的每一个词赋值为1
        for (String word : words) {

            context.write(new Text(word), new IntWritable(1));
        }
    }

}

3.Reduce

Reduce当然也是省略掉一系列不必要的操作,将Map传过来的数据直接根据自己的需求处理就好

//四个参数为Map传下来的key和value
           Reduce传出去的key和value
     public class WordcountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{

     着重需要注意的是,**传过来的数据已经将相同的key分好了,所以values是一个可以遍历的集合**

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {


        int count = 0;

        Iterator<IntWritable> iterator = values.iterator();//将传过来的value进行根据自己的需求进行处理
        while(iterator.hasNext()){

            IntWritable value = iterator.next();
            count += value.get();
        }


        context.write(key, new IntWritable(count));

    }



}

4.实现使用框架再集群上运行
(1)LinuxToYarn(在linux上提交到yarn上),首先将程序打包,项目右击–>export–>JAR file —>finish .
打包之后放到linux中执行命令 hadoop jar xx.jar 项目的全路径就OK

说下为什么这样可以执行
**
* 如果要在hadoop集群的某台机器上启动这个job提交客户端的话
* conf里面就不需要指定 fs.defaultFS mapreduce.framework.name
*
* 因为在集群机器上用 hadoop jar xx.jar cn.edu360.mr.wc.JobSubmitter2 命令来启动客户端main方法时,
* hadoop jar这个命令会将所在机器上的hadoop安装目录中的jar包和配置文件加入到运行时的classpath中
*
* 那么,我们的客户端main方法中的new Configuration()语句就会加载classpath中的配置文件,自然就有了
* fs.defaultFS 和 mapreduce.framework.name 和 yarn.resourcemanager.hostname 这些参数配置
*

public class Driver {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        //conf.set("fs.defaultFS", "hdfs://hadoop01:9000");
        Job job = Job.getInstance(conf);

        //设置job的map和reduce是哪一个,并且设置是哪一做任务提交
        job.setMapperClass(MapTask.class);
        job.setReducerClass(ReduceTask.class);
        job.setJarByClass(Driver.class);

        //设置输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //设置输入和输出目录
        FileInputFormat.addInputPath(job, new Path("/wc.txt"));
        FileOutputFormat.setOutputPath(job, new Path("/wordcount/wc-output"));
        // 提交之后会监控运行状态
        boolean completion = job.waitForCompletion(true);
        System.out.println(completion?"程序执行完毕,没毛病!!!":"程序有问题,程序出bug了,赶紧加班调试!!!");

    }

}

(2)
在本地上模拟运行 ,调试的时候可以用这种方式,效率较高,如果有这个错误的话,在windows本地环境变量里面的PATH配置环境变量(E:\hadoop\hadoop-2.8.3\bin)这里写图片描述

public static void main(String[] args) throws Exception {
        //声明使用哪个用户提交的
        System.setProperty("HADOOP_USER_NAME", "root");
        Configuration conf = new Configuration();

        /**
         * fs.defaultFS   默认值 file:///    本地文件系统
         * mapreduce.framework.name   默认值  local
         */
        Job job = Job.getInstance(conf, "WindowsToWindowsLocal");

        //设置map和reduce,以及提交的jar
        job.setMapperClass(WordcountMapper.class);
        job.setReducerClass(WordcountReducer.class);
        job.setJarByClass(Driver.class);

        //设置输入输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //输入和输出目录
        FileInputFormat.addInputPath(job, new Path("E:\\data\\word.txt"));
        FileOutputFormat.setOutputPath(job, new Path("d:\\data\\out\\wc"));

        //判断文件是否存在
        File file = new File("d:\\data\\out\\wc");
        if(file.exists()){
            FileUtils.deleteDirectory(file);
        }

        //提交任务
        boolean completion = job.waitForCompletion(true);
        System.out.println(completion?0:1);

    }

(3)在eclipse中运行

 public static void main(String[] args) {
       try {
        // 在代码中设置JVM系统参数,用于给job对象来获取访问HDFS的用户身份
            System.setProperty("HADOOP_USER_NAME", "root");


            Configuration conf = new Configuration();
            // 1、设置job运行时要访问的默认文件系统
            conf.set("fs.defaultFS", "hdfs://hadoop01:9000");
            // 2、设置job提交到哪去运行
            conf.set("mapreduce.framework.name", "yarn");
            conf.set("yarn.resourcemanager.hostname", "hadoop01");
            // 3、如果要从windows系统上运行这个job提交客户端程序,则需要加这个跨平台提交的参数
            conf.set("mapreduce.app-submission.cross-platform","true");


           Job job = Job.getInstance(conf);
         // 1、封装参数:jar包所在的位置
           job.setJar("C:\\Users\\Hailong\\Desktop\\w2.jar"); 
           job.setMapperClass(WordcountMapper.class);
           job.setReducerClass(WordcountReducer.class);

           job.setMapOutputKeyClass(Text.class);
           job.setMapOutputValueClass(IntWritable.class);
           job.setOutputKeyClass(Text.class);
           job.setOutputValueClass(IntWritable.class);

           FileSystem fs = FileSystem.get(conf);
           if(!fs.exists(new Path("/wordcount/eclipse-out/"))) {
              fs.delete(new Path("/wordcount/eclipse-out/"),true);
           }

           FileInputFormat.addInputPath(job, new Path("/wc.txt"));
           FileOutputFormat.setOutputPath(job, new Path("/wordcount/eclipse-out/"));

            // 5、封装参数:想要启动的reduce task的数量
            job.setNumReduceTasks(2);

           boolean completion = job.waitForCompletion(true);
            System.out.println(completion?"程序执行完毕,没毛病!!!":"程序有问题,程序出bug了,赶紧加班调试!!!");
       }
       catch(Exception e) {

       }
}

这里写图片描述

NodeManager资源总量配置一定大于等于1.5G

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值