美文网首页
MapReduce 基础 (三)分区

MapReduce 基础 (三)分区

作者: 做个合格的大厂程序员 | 来源:发表于2020-06-17 11:10 被阅读0次

概念

在 MapReduce 中, 通过我们指定分区, 会将同一个分区的数据发送到同一个 Reduce 当中进行 处理

例如: 为了数据的统计, 可以把一批类似的数据发送到同一个 Reduce 当中, 在同一个 Reduce 当 中统计相同类型的数据, 就可以实现类似的数据分区和统计等

其实就是相同类型的数据, 有共性的数据, 送到一起去处理

Reduce 当中默认的分区只有一个

image

案例:
表中的数据分区成两组,一组是中奖结果大于15的分区查询,一组的中奖结果小于15的分区查询。

PartitionReducer

package Partitioner;

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

import java.io.IOException;

public class PartitionReducer extends Reducer<Text, NullWritable, Text,NullWritable> {
    @Override
    protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        context.write(key,NullWritable.get());
    }
}

PartitionMapper

package Partitioner;

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

import java.io.IOException;

public class PartitionMapper extends Mapper<LongWritable,Text, Text, NullWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        context.write(value,NullWritable.get());
    }
}

Partition

package Partitioner;

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

public class PartitonerOwn extends Partitioner<Text, NullWritable> {

    @Override
    public int Partition(Text text, NullWritable nullWritable, int i) {
        //1:拆分行文本数据(K2),获取中奖字段的值
        String[] split = text.toString().split("\t");
        String numStr = split[5];

        //2:判断中奖字段的值和15的关系,然后返回对应的分区编号
        if(Integer.parseInt(numStr) > 15){
            return  1;
        }else{
            return  0;
        }
    }
}

main

package Partitioner;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.net.URI;

public class MainJob extends Configured implements Tool {

    @Override
    public int run(String[] strings) throws Exception {

        //1.创建job任务对象
        Job job = Job.getInstance(super.getConf(), "partition_mapreduce");

        //2.对job任务进行配置(八个步骤)

        //1)设置输入类和路径
        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job,new Path("hdfs://node1:8020/input"));
        
        //2) 设置mapper类
        job.setMapperClass(PartitionerMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);
        
        //3) 指定分区类
        job.setPartitionerClass(PartitonerOwn.class);
        
        //7) 指定Reducer类
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);
        
        //设置reduceTask个数
        job.setNumReduceTasks(2);

        //8)指定输出类和路径
        Path path = new Path("hdfs://node1:8020/out/partition_out");
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job,path);

        FileSystem fileSystem = FileSystem.get(new URI("hdfs://node1:8020/out/partition_out"),new Configuration());
        if (fileSystem.exists(path)){
            fileSystem.delete(path,true);
        }

        //3.等待任务执行
        boolean bl = job.waitForCompletion(true);
        return bl?0:1;
    }

    public static void main(String[] args) throws Exception{
        Configuration configuration = new Configuration();
        
        //启动Job任务
        int run = ToolRunner.run(configuration,new MainJob(),args);

        System.exit(run);
    }
}

相关文章

  • MapReduce 基础 (三)分区

    概念 在 MapReduce 中, 通过我们指定分区, 会将同一个分区的数据发送到同一个 Reduce 当中进行 ...

  • MapReduce分区组件

    MapReduce中分区组件 需求: 根据单词的长度给单词出现的次数的结果存储到不同文件中,以便于在快速查询 思路...

  • MapReduce的分区

    1.分区涉及到的是多个reduceTask如果只有1个或0个 , 那么就不存在分区 1.在没有自定义分区的时候,系...

  • 【复习001】-20170722

    一、大数据基础 二、MapReduce和Yarn功能与架构Yarn(集群资源管理)的组件 三、MapReduce客...

  • [ITSTAR]第三课:Hadoop的背景起源二:MapRedu

    MapReduce模型: MapReduce基础编程模型: PageRank(搜索排名): 详细内容:

  • mapreduce自定义partition分区

    一、背景 背景继续上一篇文章《记一次mapreduce的简单优化》。在那篇文章中提到了mapreduce自带分区方...

  • Hadoop生态学习之Hadoop集群搭建

    hadoop集群基础环境搭建 注意:基础的Hadoop集群只包含HDFS、YARN、MapReduce三个基本组件...

  • 大数据基础之Hadoop和Spark

    大数据基础 Hadoop背景起源一 HDFS Hadoop背景起源二 MapReduce Hadoop背景起源三 ...

  • 1. hdfs实例

    hadoop包含hdfs和MapReduce,hdfs负责数据文件存储,是hadoop的基础,MapReduce是...

  • 大数据学习之Hadoop

    一、基础概念 Hadoop包含HDFS和MapReduce,HDFS实现分布式存储,MapReduce实现数据分布...

网友评论

      本文标题:MapReduce 基础 (三)分区

      本文链接:https://www.haomeiwen.com/subject/krwjxktx.html