前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MapReduce快速入门系列(8) | Shuffle之排序(sort)——区内排序

MapReduce快速入门系列(8) | Shuffle之排序(sort)——区内排序

作者头像
不温卜火
发布2020-10-28 15:29:07
3360
发布2020-10-28 15:29:07
举报
文章被收录于专栏:不温卜火

上一篇博文讲了Shuffle排序的相关概念以及全排序的操作,这篇博文继续分享的是排序的另一种操作:区内排序。

一. 需求分析

  基于前一个需求,增加自定义分区类,分区按照省份手机号设置。

  • 1. 把原数据排序后
1
1
  • 2. 期望数据输出
2
2

二. 代码实现

2.1 增加自定义分区类MyPartitioner2

代码语言:javascript
复制
package com.buwenbuhuo.WritableComparable2;

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

/**
 * @author 卜温不火
 * @create 2020-04-24 18:14
 * com.buwenbuhuo.WritableComparable2 - the name of the target package where the new class or interface will be created.
 * mapreduce0422 - the name of the current project.
 */
public class MyPartitioner2 extends Partitioner<com.buwenbuhuo.WritableComparable.FlowBean, Text> {
    @Override
    public int getPartition(com.buwenbuhuo.WritableComparable.FlowBean flowBean, Text text, int numPartitions) {
        switch (text.toString().substring(0, 3)) {
            case "136":
                return 0;
            case "137":
                return 1;
            case "138":
                return 2;
            case "139":
                return 3;
            default:
                return 4;
        }
    }
}

2.2 在驱动类中添加分区类

代码语言:javascript
复制
// 加载自定义分区类
job.setPartitionerClass(ProvincePartitioner.class);

// 设置Reducetask个数
job.setNumReduceTasks(5);
  • 此部分的完整代码如下:
代码语言:javascript
复制
package com.buwenbuhuo.WritableComparable2;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;

/**
 * @author 卜温不火
 * @create 2020-04-24 18:19
 * com.buwenbuhuo.WritableComparable2 - the name of the target package where the new class or interface will be created.
 * mapreduce0422 - the name of the current project.
 */
public class SortDriver {

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

        job.setJarByClass(com.buwenbuhuo.WritableComparable2.SortDriver.class);
        job.setMapperClass(com.buwenbuhuo.WritableComparable.SortMapper.class);
        job.setReducerClass(com.buwenbuhuo.WritableComparable.SortReducer.class);

        job.setMapOutputKeyClass(com.buwenbuhuo.WritableComparable.FlowBean.class);
        job.setMapOutputValueClass(Text.class);

        job.setPartitionerClass(MyPartitioner2.class);
        job.setNumReduceTasks(5);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(com.buwenbuhuo.WritableComparable.FlowBean.class);

        FileInputFormat.setInputPaths(job, new Path("d:\\output"));
        FileOutputFormat.setOutputPath(job, new Path("d:\\output2"));

        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }
}

三. 运行及其结果

  • 1. 运行
3
3
  • 2. 结果
4
4
5
5
在这里插入图片描述
在这里插入图片描述
6
6
7
7
  • 3. 与设想的对比
8
8

可以看到是一样的。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/04/27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一. 需求分析
  • 二. 代码实现
    • 2.1 增加自定义分区类MyPartitioner2
      • 2.2 在驱动类中添加分区类
      • 三. 运行及其结果
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档