MapReduce(Hadoop入门四)

MapReduce什么是MapReduceMapReduce是由两部分构成,即Map和Reduce分别对应的实现类时Mapper和Reducer。官网(http://hadoop.apache.org/docs/stable/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html) 中这么介绍的...
摘要由CSDN通过智能技术生成

什么是MapReduce

MapReduce是由两部分构成,即Map和Reduce分别对应的实现类时Mapper和Reducer。官网 中这么介绍的

Hadoop MapReduce is a software framework for easily writing applications which process vast amounts of data (multi-terabyte data-sets) in-parallel on large clusters (thousands of nodes) of commodity hardware in a reliable, fault-tolerant manner.

简单来说就是MapReduce是一个用于在大型集群中可以对TB规模数据处理的计算框架,并且编程起来很easy。之所以简单是因为MapReduce有着固定的“套路”。

MapReduce编程套路

MapReduce在编程时有三个必不可少的组成:Mapper,Reducer和Driver。分别负责对数据的分区映射,排序合并以及驱动程序。

Mapper

写MR程序时,需要自己自定义一个Mapper类并且继承org.apache.hadoop.mapreduce 的 Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>,同时还得有map函数

protected void map(KEYIN key, VALUEIN value, Mapper<KEYIN, VALUEIN, KEYOUT, VALUEOUT>.Context context) throws IOException, InterruptedException {
        context.write(key, value);
    }

Reducer

有了Mapper之后还得有Reducer将数据聚合处理,自定义Reducer类继承org.apache.hadoop.mapreduce 的 Reducer<KEYIN, VALUEIN, KEYOUT, VALUEOUT>,同时还得有reduce函数

protected void reduce(KEYIN key, Iterable<VALUEIN> values, Reducer<KEYIN, VALUEIN, KEYOUT, VALUEOUT>.Context context) throws IOException, InterruptedException {
    Iterator i$ = values.iterator();

    while(i$.hasNext()) {
        VALUEIN value = i$.next();
        context.write(key, value);
    }

}

Driver

作为MapReduce的驱动程序,需要多个步骤,下面介绍一般程序都有的步骤。

  • 创建环境Configuration如果有需要还可以设置相应的环境变量,通过应用变量添加新的变量或者覆盖原先配置的xml文件中的变量,例如可以通过设置configuration.setBoolean(Job.MAP_OUTPUT_COMPRESS,true)让map输出进行压缩,可以减少磁盘空间,减少网络传输。

  • 创建作业Job,Job通过静态方法getInstance()得到Job实例,该方法根据不同的参数有不同的实现,例如:

    public static Job getInstance(Configuration conf) throws IOException {
        JobConf jobConf = new JobConf(conf);
        return new Job(jobConf);
    }
    
  • 设置作业的驱动类

    public void setJarByClass(Class<?> cls) {
        this.ensureState(Job.JobState.DEFINE);
        this.conf.setJarByClass(cls);
    }
    
  • 设置作业的Mapper类和Reducer类

    //设置Mapper
    public void setMapperClass(Class<? extends Mapper> cls) throws IllegalStateException {
        this.ensureState(Job.JobState.DEFINE);
        this.conf.setClass("mapreduce.job.map.class", cls, Mapper.class);
    }
    //设置Reducer
    public void setReducerClass(Class<? extends Reducer> cls) throws IllegalStateException {
        this.ensureState(Job.JobState.DEFINE);
        this.conf.setClass("mapreduce.job.reduce.class", cls, Reducer.class);
    }
    
  • 设置Mapper的输出键-值对

    //键
    public void setMapOutputKeyClass(Class<?> theClass) throws IllegalStateException {
        this.ensureState(Job.JobState.DEFINE);
        this.conf.setMapOutputKeyClass(theClass);
    }
    //值
    public void setMapOutputValueClass(Class<?> theClass) throws IllegalStateException {
        this.ensureState(Job.JobState.DEFINE);
        this.conf.setMapOutputValueClass(theClass);
    }
    
  • 设置Reducer的输出键-值对

    public void setOutputKeyClass(Class<?> theClass) throws IllegalStateException {
       this.ensureState(Job.JobState.DEFINE);
       this.conf.setOutputKeyClass(theClass);
    }
    
    public void setOutputValueClass(Class<?> theClass) throws IllegalStateException {
       this.ensureState(Job.JobState.DEFINE);
       this.conf.setOutputValueClass(theClass);
    }
    
  • 设置输入输出路径
    输入输出路径分别由FileInputFormatFileOutputFormat的静态方法setInputPaths()setOut

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值