1.计数器:可以让开发人员以全局的视角来审查程序运行状况和各个指标。

   获得计数器:Conter myConter = config.getConter("组的名字","计数器名");

   为计数器设置初值:myConter.setValue(初始值);

   增加:myConter.increment();

2.Combiners(规约)

   每一个map会产生大量的输出,combiner的作用就是在map端对输出做一次合并,以减少到reduce的数据量,网络传输少。

   只能在本地map中进行合并,并不能跨map执行,所以还需要reduce

   combiner是选配的,因为对于某些逻辑,使用前与使用后的计算结果不一致。

   

   job.setCombinerClass(MyReduce.class);

   

3.Partitioner(分组)

       1.mapreduce的默认partitioner是HashPartitioner

       2.自定义

   class KpiPartitioner extends Partitioner<Text, KpiWritable>{

       @Override

       public int getPartition(Text key, LongWritable value, int numPartitions) {

          return (key.toString().length()==11)?0:1;

       }

}

然后在main方法中加入

   job.setPartitionerClass(KpiPartitioner.class);

   job.setNumberReduceTasks(2);


4.排序和分组

   1.在map和reduce阶段进行排序时,比较的是k2,v2是不参与排序比较的,如果想让v2参与排序,需要把k2和v2组装成新的类,作为k2,才能比较。

   2.分组也是按照k2进行的。

class NewGroup implements RawComparator<NewKey>{

   /**

    * 比较字节数组中指定的字节序列的大小

    * b1: 第一个参与比较的数组

    * b2:第二个参与比较的数组

    *

    * s1:第一个参与比较的字节数组的开始位置

    * s2:第二个

    *

    * l1:比较长度

    */

   @Override

   public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {

   

       return WritableComparator.compareBytes(b1, s1, 8, b2, s2, 8);

   }

   

       @Override

       public int compare(NewKey o1, NewKey o2) {

       // TODO Auto-generated method stub

       return 0;

   }

}

然后在main中

job.setGroupingComparatorClass(NewGroup.class).