mapreduce | 自定义Partition分区(案例1)

1.需求

将学生成绩,按照各个成绩降序排序,各个科目成绩单独输出。

# 自定义partition 将下面数据分区处理:

人名 科目 成绩

张三 语文 10

李四 数学 30

王五 语文 20

赵6 英语 40

张三 数据 50

李四 语文 10

张三 英语 70

李四 英语 80

王五 英语 45

王五 数学 10

赵6 数学 10

赵6 语文 100

2.思路分析

# 自定义分区

1. 编写自定义分区类,继承Partitioner覆盖getPartition方法 注意:分区号从0开始算。

2. 给job注册分区类 【覆盖默认分区】 job.setPartitionerClass(自定义Partitioner.class); 3. 设置ReduceTask个数(开启分区) job.setNumReduceTasks(数字);//reduceTask数量要和分区数量一样。

3.Idea代码

DefinePartitionJob

package demo7;

import demo5.DescIntWritable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class DefinePartitionJob {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop10:8020");

        Job job = Job.getInstance(conf);
        job.setJarByClass(DefinePartitionJob.class);

        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);

        TextInputFormat.addInputPath(job,new Path("/mapreduce/demo10"));
        TextOutputFormat.setOutputPath(job,new Path("/mapreduce/demo10/out"));

        job.setMapperClass(DefinePartitonMapper.class);
        job.setReducerClass(DefinePartitonReducer.class);
        //map输出的键与值类型
        job.setMapOutputKeyClass(DescIntWritable.class);
        job.setMapOutputValueClass(Subject.class);
        //reducer输出的键与值类型
        job.setOutputKeyClass(Subject.class);
        job.setOutputValueClass(DescIntWritable.class);

        //设置reduceTask的个数
        job.setNumReduceTasks(4);
        //设置自定义分区
        job.setPartitionerClass(MyPartition.class);

        boolean b = job.waitForCompletion(true);
        System.out.println(b);

    }


    static class DefinePartitonMapper extends Mapper<LongWritable, Text, DescIntWritable,Subject> {
        @Override
        protected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException {
            String[] arr = value.toString().split("\t");
            context.write(new DescIntWritable(Integer.parseInt(arr[2])),new Subject(arr[0],arr[1]));
        }
    }
    static class DefinePartitonReducer extends Reducer<DescIntWritable,Subject,Subject,DescIntWritable> {
        @Override
        protected void reduce(DescIntWritable key, Iterable<Subject> values, Context context) throws IOException, InterruptedException {
            for (Subject subject : values) {
                context.write(subject, key);
            }
        }
    }}

MyPartition

package demo7;

import demo5.DescIntWritable;
import org.apache.hadoop.mapreduce.Partitioner;

public class MyPartition extends Partitioner<DescIntWritable,Subject> {
    @Override
    public int getPartition(DescIntWritable key, Subject value, int numPartitions) {
        if ("语文".equals(value.getKemu())){
            return 0;
        }else if ("数学".equals(value.getKemu())) {
            return 1;
        }else if ("英语".equals(value.getKemu())) {
            return 2;
        }
            return 3;


        }
}

 Subject

package demo7;

import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class Subject implements Writable{
    private String name;
    private String kemu;

    public Subject() {
    }

    public Subject(String name, String kemu) {
        this.name = name;
        this.kemu = kemu;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getKemu() {
        return kemu;
    }

    public void setKemu(String kemu) {
        this.kemu = kemu;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(name);
        out.writeUTF(kemu);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.name = in.readUTF();
        this.kemu = in.readUTF();

    }

    @Override
    public String toString() {
        return name + " " +kemu;
    }
}

 4.在hdfs查看结果


不要去争辩,多提升自己~

  • 17
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MapReduce是一种大数据处理框架,它能够在分布式集群上进行并行计算。其中,自定义比较器是MapReduce的一种功能,允许用户定义自己的比较方法来排序输出结果。 使用自定义比较器可以实现复杂的排序逻辑,而不是仅仅使用基本的字典序排序。例如,可以使用自定义比较器来按照日期、数字或其他自定义字段排序。 使用自定义比较器的方法是在MapReduce程序中实现自定义比较器类,并实现其中的compare方法。然后,在MapReduce作业的配置中设置自定义比较器类。 例如,以下是一个使用自定义比较器的MapReduce程序的示例: ```java import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount { public static class MyMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } } public static class MyReducer extends Reducer<Text,IntWritable,Text,IntWritable>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呆呆不呆~

你的鼓励是我最开心的事情~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值