MapReduce如何自定义分区

我任然套用之前发过的一个MapReduce例子来演示如何自定义分区,大家可以先去看一下我之前的例子
https://blog.csdn.net/dudadudadd/article/details/111867084
下面沿用上面的例子,在pom和代码不变的情况下,做一个自定义分区操作,目的是根据手机号前三位的不同分配数据到不同分区,如此我使用如下自定义分区类

package com.wy;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Partitioner;
/**
*继承的类也是hadoop的jar有的泛型要和作用的map段的输入保持一致
在类中需要注意的是,分区是从零开始的
*/
public class Par extends Partitioner<TeleptoneBean, NullWritable> {
    @Override
    public int getPartition(TeleptoneBean teleptoneBean, NullWritable nullWritable, int i) {
        String substring = teleptoneBean.getTelephone().substring(0, 3);
        if (substring.equals("139")) {
            return 0;
        } else if (substring.equals("138")) {
            return 1;
        } else if (substring.equals("135")) {
            return 2;
        }else {
            return 3;
        }
    }
}

之后其实都简单了,我们只需要在Driver类里面设置两个东西就可以了,其他的东西都不变

	//job 工作完成流程
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        //创建一个提交作业需要的配置对象,我们现在
        //不更改它,使用它里面默认的配置
        Configuration cfg = new Configuration();
        //生成任务对象
        Job job = Job.getInstance(cfg);
        //修改任务所在的Driver类
        job.setJarByClass(TeleptoneDrvier.class);
        //对输出参数设置
        job.setOutputKeyClass(TeleptoneBean.class);
        job.setOutputValueClass(NullWritable.class);
        //对输出参数配置
        job.setMapOutputKeyClass(TeleptoneBean.class);
        job.setMapOutputValueClass(NullWritable.class);
        //设置map reduce类
        job.setMapperClass(TeleptoneMapper.class);
        job.setReducerClass(TeleptoneReduce.class);

        /**
         * 只需要添加这两行代码就可以了
         * 用来決定分区策略的类和分区数
         * 这里要注意的是
         * setNumReduceTasks方法
         * 的值必须大于或等于自定义分区类中的代码逻辑里面的最大分区,同时这里说的最大分区是分区的个数而不是最大分区的下标
         * 不然任务会报错,大于事多余的分区任然会创建只是没有数据为空而已
         */
        job.setPartitionerClass(Par.class);
        job.setNumReduceTasks(4);

        //设置输入输出路径
        FileInputFormat.setInputPaths(job, new Path("D:\\a\\inputphone"));
        FileOutputFormat.setOutputPath(job, new Path("D:\\a\\outputphone"));
        
        //退出,单mapreduce这样写如果是多个MapReduce则有些不同了,我会在补充,这里大家先知道单MapReduce怎么写的
        boolean b = job.waitForCompletion(true);
        System.exit(b == true ? 0 : -1);


    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MapReduce是一种用于处理大规模数据集的编程模型和软件框架。Hadoop是一个基于MapReduce模型的分布式文件存储和处理系统。在Hadoop中,MapReduce被广泛用于数据处理和分析任务。 自定义二次排序是MapReduce中常见的一种需求,其目的是对MapReduce的输出进行排序。下面我们来介绍一下如何在Linux上使用Hadoop实现自定义二次排序。 1. 准备数据 首先我们需要准备一个数据集,假设我们有一个文本文件,每行包含两个字段,分别为学生姓名和成绩,中间用制表符分隔。例如: ``` Tom 80 Jerry 70 Mike 90 Lucy 85 ``` 2. 编写Mapper代码 自定义二次排序需要进行两次排序,第一次按照学生姓名进行排序,第二次按照成绩进行排序。因此,我们需要在Mapper中将学生姓名和成绩作为Key-Value输出。 我们可以使用TextPair类来存储学生姓名和成绩,代码如下: ``` public class SortMapper extends Mapper<LongWritable, Text, TextPair, Text> { private TextPair pair = new TextPair(); public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] fields = value.toString().split("\t"); pair.set(fields[0], fields[1]); context.write(pair, value); } } ``` 在这段代码中,我们首先将输入的一行数据拆分成学生姓名和成绩两个字段,然后使用TextPair类将它们作为Key输出,原始数据作为Value输出。 3. 编写Partitioner代码 Partitioner用于对Mapper的输出进行分区,以确保相同Key的数据被分配到同一个Reducer中。在自定义二次排序中,我们需要按照学生姓名进行分区,因此我们可以使用HashPartitioner来进行分区,代码如下: ``` public class SortPartitioner extends Partitioner<TextPair, Text> { public int getPartition(TextPair key, Text value, int numPartitions) { return (key.getFirst().hashCode() & Integer.MAX_VALUE) % numPartitions; } } ``` 在这段代码中,我们使用HashPartitioner将学生姓名的HashCode和Partition数取模来确定数据被分配到哪个Reducer中。 4. 编写GroupComparator代码 GroupComparator用于将相同学生姓名的数据分配到同一个Reducer中,代码如下: ``` public class SortGroupComparator extends WritableComparator { protected SortGroupComparator() { super(TextPair.class, true); } public int compare(WritableComparable a, WritableComparable b) { TextPair pair1 = (TextPair) a; TextPair pair2 = (TextPair) b; return pair1.getFirst().compareTo(pair2.getFirst()); } } ``` 在这段代码中,我们重载了compare方法,用于比较两个Key的学生姓名是否相同。 5. 编写SortComparator代码 SortComparator用于对每个Reducer中的数据进行排序,按照成绩从大到小排序,代码如下: ``` public class SortComparator extends WritableComparator { protected SortComparator() { super(TextPair.class, true); } public int compare(WritableComparable a, WritableComparable b) { TextPair pair1 = (TextPair) a; TextPair pair2 = (TextPair) b; int cmp = pair1.getFirst().compareTo(pair2.getFirst()); if (cmp != 0) { return cmp; } return -pair1.getSecond().compareTo(pair2.getSecond()); } } ``` 在这段代码中,我们首先比较两个Key的学生姓名是否相同,如果相同则比较成绩,否则直接返回姓名比较结果。 6. 编写Reducer代码 Reducer用于对Mapper的输出进行聚合和处理。在自定义二次排序中,我们只需要将每个学生的成绩按照从高到低的顺序输出即可,代码如下: ``` public class SortReducer extends Reducer<TextPair, Text, Text, Text> { public void reduce(TextPair key, Iterable<Text> values, Context context) throws IOException, InterruptedException { for (Text value : values) { context.write(key.getFirst(), value); } } } ``` 在这段代码中,我们首先输出学生姓名,然后按照原始数据的顺序输出。 7. 编写Driver代码 最后,我们需要编写Driver代码来启动MapReduce作业。代码如下: ``` public class SortDriver extends Configured implements Tool { public int run(String[] args) throws Exception { Job job = Job.getInstance(getConf()); job.setJarByClass(SortDriver.class); job.setMapperClass(SortMapper.class); job.setPartitionerClass(SortPartitioner.class); job.setGroupingComparatorClass(SortGroupComparator.class); job.setSortComparatorClass(SortComparator.class); job.setReducerClass(SortReducer.class); job.setMapOutputKeyClass(TextPair.class); job.setMapOutputValueClass(Text.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); FileInputFormat.setInputPaths(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); return job.waitForCompletion(true) ? 0 : 1; } public static void main(String[] args) throws Exception { int exitCode = ToolRunner.run(new SortDriver(), args); System.exit(exitCode); } } ``` 在这段代码中,我们首先创建一个Job实例,然后设置Mapper、Partitioner、GroupComparator、SortComparator和Reducer等类。最后,我们指定输入路径和输出路径,并启动作业。 以上就是在Linux上使用Hadoop实现自定义二次排序的流程。通过这个例子,您可以了解到如何在Linux系统上使用MapReduce编程模型和Hadoop分布式文件存储和处理系统来处理大规模数据集。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值