hadoop学习:mapreduce入门案例四:partitioner 和 combiner

这篇具有很好参考价值的文章主要介绍了hadoop学习:mapreduce入门案例四:partitioner 和 combiner。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

先简单介绍一下partitioner 和 combiner 

Partitioner类

  • 用于在Map端对key进行分区
    • 默认使用的是HashPartitioner
      • 获取key的哈希值
      • 使用key的哈希值对Reduce任务数求模
    • 决定每条记录应该送到哪个Reducer处理
  • 自定义Partitioner
    • 继承抽象类Partitioner,重写getPartition方法
    • job.setPartitionerClass(MyPartitioner.class)

Combiner类

  • Combiner相当于本地化的Reduce操作
    • 在shuffle之前进行本地聚合
    • 用于性能优化,可选项
    • 输入和输出类型一致
  • Reducer可以被用作Combiner的条件
    • 符合交换律和结合律
  • 实现Combiner
    • job.setCombinerClass(WCReducer.class)

我们进入案例来看这两个知识点

一 案例需求

一个存放电话号码的文本,我们需要136 137,138 139和其它开头的号码分开存放统计其每个数字开头的号码个数

hadoop学习:mapreduce入门案例四:partitioner 和 combiner,mapreduce,mapreduce,大数据,hadoop,学习,linux

hadoop学习:mapreduce入门案例四:partitioner 和 combiner,mapreduce,mapreduce,大数据,hadoop,学习,linux
效果

 二 PhoneMapper 类

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class PhoneMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String phone = value.toString();
        Text text = new Text(phone);
        IntWritable intWritable = new IntWritable(1);
        context.write(text,intWritable);
    }
}

三 PhoneReducer 类

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class PhoneReducer extends Reducer<Text, IntWritable,Text,IntWritable> {
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int count = 0;
        for (IntWritable intWritable : values){
            count += intWritable.get();
        }
        context.write(key, new IntWritable(count));
    }
}

四 PhonePartitioner 类

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

public class PhonePartitioner extends Partitioner<Text, IntWritable> {
    @Override
    public int getPartition(Text text, IntWritable intWritable, int i) {
        //136,137   138,139     其它号码放一起
        if("136".equals(text.toString().substring(0,3)) || "137".equals(text.toString().substring(0,3))){
            return 0;
        }else if ("138".equals(text.toString().substring(0,3)) || "139".equals(text.toString().substring(0,3))){
            return 1;
        }else {
            return 2;
        }

    }
}

五 PhoneCombiner 类

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class PhoneCombiner extends Reducer<Text, IntWritable,Text,IntWritable> {
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int count = 0;
        for(IntWritable intWritable : values){
            count += intWritable.get();
        }
        context.write(new Text(key.toString().substring(0,3)), new IntWritable(count));
    }
}

六 PhoneDriver 类

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class PhoneDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(PhoneDriver.class);

        job.setMapperClass(PhoneMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setCombinerClass(PhoneCombiner.class);

        job.setPartitionerClass(PhonePartitioner.class);
        job.setNumReduceTasks(3);

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

        Path inPath = new Path("in/demo4/phone.csv");
        FileInputFormat.setInputPaths(job, inPath);

        Path outPath = new Path("out/out6");
        FileSystem fs = FileSystem.get(outPath.toUri(),conf);
        if (fs.exists(outPath)){
            fs.delete(outPath, true);
        }
        FileOutputFormat.setOutputPath(job, outPath);

        job.waitForCompletion(true);

    }
}

七 小结

该案例新知识点在于分区(partition)和结合(combine)

这次代码的流程是 

driver——》mapper——》partitioner——》combiner——》reducer

map 每处理一条数据都经过一次 partitioner 分区然后存到环形缓存区中去,然后map再去处理下一条数据以此反复直至所有数据处理完成

combine 则是将环形缓存区溢出的缓存文件合并,并提前进行一次排序和计算(对每个溢出文件计算后再合并)最后将一个大的文件给到 reducer,这样大大减少了 reducer 的计算负担文章来源地址https://www.toymoban.com/news/detail-685798.html

到了这里,关于hadoop学习:mapreduce入门案例四:partitioner 和 combiner的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Hadoop入门学习笔记——四、MapReduce的框架配置和YARN的部署

    视频课程地址:https://www.bilibili.com/video/BV1WY4y197g7 课程资料链接:https://pan.baidu.com/s/15KpnWeKpvExpKmOC8xjmtQ?pwd=5ay8 Hadoop入门学习笔记(汇总) 本次YARN的部署结构如下图所示: 当前,共有三台服务器(虚拟机)构成集群,集群规划如下所示: 主机 部署的服务 node1 ResourceManager、N

    2024年02月04日
    浏览(35)
  • Hadoop3 - MapReduce COVID-19 案例实践

    上篇文章对 MapReduce 进行了介绍,并编写了 WordCount 经典案例的实现,本篇为继续加深 MapReduce 的用法,实践 COVID-19 新冠肺炎案例,下面是上篇文章的地址: https://blog.csdn.net/qq_43692950/article/details/127195121 COVID-19,简称“新冠肺炎”,世界卫生组织命名为“2019冠状病毒病” [1-

    2024年02月08日
    浏览(31)
  • 虚拟机+Hadoop下MapReduce的Wordcount案例

    环境:ubuntu18.04 前提:Hadoop已经搭建好 抄作业记得改标题 输入内容(可以自定义,抄作业别写一样的) yarn-site.xml 内容如下,注意第一个property要改: ·输入hadoop classpath(任意路径下均可),将返回的内容复制在第一个property的value中 *修改配置文件之后要重启hadoop(关了又

    2024年02月07日
    浏览(39)
  • 【Hadoop_06】MapReduce的概述与wc案例

    MapReduce是一个 分布式运算程序 的编程框架,是用户开发“基于Hadoop的数据分析应用”的核心框架。 MapReduce核心功能是 将用户编写的业务逻辑代码 和 自带默认组件 整合成一个完整的 分布式运算程序 ,并发运行在一个Hadoop集群上。 1)MapReduce易于编程 它简单的实现一些接口

    2024年02月04日
    浏览(36)
  • Hadoop3教程(十九):MapReduce之ETL清洗案例

    ETL,即 Extract-Transform-Load 的缩写,用来描述数据从源端,经过抽取(Extract)、转换(transform),最后加载(load)到目标端的处理过程。 ETL主要应用于数据仓库,但不只是应用于数据仓库,毕竟这个更像是一类思想。 在运行核心的MR程序之前,往往要对数据进行清理,清除掉

    2024年02月06日
    浏览(35)
  • Hadoop3教程(十七):MapReduce之ReduceJoin案例分析

    现在有两个文件: orders.txt,存放的是订单ID、产品ID、产品数量 pd.txt,这是一个产品码表,存放的是产品ID、产品中文名; 现在是想通过join,来实现这么一个预期输出,即订单ID、产品中文名、产品数量。 以上是本次案例需求。 简单思考一下思路。我们需要将关联条件作为

    2024年02月07日
    浏览(36)
  • 大数据实战——基于Hadoop的Mapreduce编程实践案例的设计与实现

    图1:MaxCompute MapReduce各个阶段思路设计 设计思路分析分为六个模块:input输入数据、splitting拆分、Mapping映射、Shuffing派发、Reducing缩减、Final result输出。 输入数据:直接读入文本不进行分片,数据项本身作为单个Map Worker的输入。 Map阶段:Map处理输入,每获取一个数字,将数

    2024年02月05日
    浏览(38)
  • 大数据与云计算——部署Hadoop集群并运行MapReduce集群案例(超级详细!)

    Linux搭建Hadoop集群(CentOS7+hadoop3.2.0+JDK1.8+Mapreduce完全分布式集群) 本文所用到的版本号: CentOS7 Hadoop3.2.0 JDK1.8 基本概念及重要性 很多小伙伴部署集群用hadoop用mapreduce,却不知道到底部署了什么,有什么用。在部署集群之前先给大家讲一下Hadoop和MapReduce的基本概念,以及它们在大

    2024年02月04日
    浏览(36)
  • MapReduce入门(一)—— MapReduce概述 + WordCount案例实操

    MapReduce知识点总览图 MapReduce 是 一个分布式运算程序的编程框架 ,是用户开发“基于 Hadoop 的数据分析应用”的核心框架。 MapReduce 核心功能是 将用户编写的业务逻辑代码 和 自带默认组件 整合成一个 完整的分布式运算程序 ,并发运行在一个 Hadoop 集群上。 1.2.1 优点 1 )M

    2023年04月21日
    浏览(31)
  • Hadoop系统应用之MapReduce相关操作【IDEA版】---经典案例“倒排索引、数据去重、TopN”

      倒排索引是文档检索系统中最常用的数据结构,被广泛应用于全文搜索引擎。倒排索引主要用来存储某个单词(或词组)在一组文档中的存储位置的映射,提供了可以根据内容来查找文档的方式,而不是根据文档来确定内容,因此称为倒排索引(Inverted Index)。带有倒排索引

    2024年02月07日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包