2023年11月26日发(作者:)

MapReduce(三):分区、排序、合并

1.分区

实现分区的步骤:

1.1先分析⼀下具体的业务逻辑,确定⼤概有多少个分区

1.2⾸先书写⼀个类,它要继承ioner这个类

1.3重写public int getPartition这个⽅法,根据具体逻辑,读数据库或者配置返回相同的数字

1.4在main⽅法中设置Partioner的类,titionerClass();

1.5设置Reducer的数量,ReduceTasks(6);

以下例⼦是统计同⼀⼿机号的上⾏流量、下⾏流量以及总流量,要求分区

import put;

public long getUpFlow() {

}

//Partition

public static class DCPartitioner extends Partitioner

{

private static Mapprovider=new HashMap();

static{

("134", 1);

("134", 1);

2.排序

排序MR默认是按key2进⾏排序的,如果想⾃定义排序规则,被排序的对象要实现WritableComparable接⼝

,在compareTo⽅法中实现排序规则(MapReduce的shuffer会⾃动调⽤这个⽅法),然后将这个对象当做k2,即可完成排序

部分代码如下:

......

---------------------------------

public class Combine {

//Map

public static class CBMapper extends Mapper

{

private Text k=new Text();

private Text v=new Text();

protected void map(Object key,Text value,Context context) throws IOException, InterruptedException

//构建job对象

Configuration conf=new Configuration();

Job job=tance(conf, "Combiner");

//设置main⽅法所在的类

ByClass();