Some custom functions in MapReduce

Source: Internet
Author: User



1. counters: allows developers to review the running status and various metrics from a global perspective.

Get counter: Conter myConter = config. getConter ("group name", "counter name ");

Set the initial value for the counter: myConter. setValue (initial value );

Added: myConter. increment ();

2. Combiners (Protocol)

Each map generates a large amount of output. The combiner is used to merge the output at the map end to reduce the data volume of reduce, reducing network transmission.

It can only be merged in a local map and cannot be executed across maps. Therefore, reduce is required.

Combiner is optional, because for some logics, the calculation results before use are inconsistent with those after use.



Job. setCombinerClass (MyReduce. class );



3. Partitioner (Group)

1. The default partitioner of mapreduce is HashPartitioner.

2. Custom

Class KpiPartitioner extends Partitioner <Text, KpiWritable> {

@ Override

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

Return (key. toString (). length () = 11 )? ;

}

}

Then add

Job. setPartitionerClass (KpiPartitioner. class );

Job. setNumberReduceTasks (2 );


4. Sorting and grouping

1. in the map and reduce phases, k2 is compared, and v2 is not involved in sorting and comparison. If v2 is to be involved in sorting, k2 and v2 need to be assembled into new classes, can be compared as k2.

2. The group is also based on k2.

Class NewGroup implements RawComparator <NewKey> {

/**

* Compare the size of the specified byte sequence in the byte array

* B1: The first array to be compared

* B2: The second array involved in the comparison

*

* S1: Start position of the first byte array to be compared

* S2: Second

*

* L1: Compare the length

*/

@ 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;

}

}

Then in main

Job. setGroupingComparatorClass (NewGroup. class ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.