Hadoop Partitioner and Custom Partitioner

Source: Internet
Author: User
Tags hash

First, Hadoop partitioner

All partitioner inherit from the abstract class Partitioner, implement Getpartition (KEY var1, VALUE var2, intvar3), and the partitioner with Hadoop comes with:

(1) Totalorderpartitioner

Generally used when doing global sorting

(2) Keyfieldbasedpartitioner

(3) Binarypartitioner
public int getpartition (binarycomparable key, V value, int numpartitions) {
    int length = Key.getlength ();
    int Leftindex = (this.leftoffset + length)% length;
    int Rightindex = (this.rightoffset + length)% length;
    int hash = writablecomparator.hashbytes (Key.getbytes (), Leftindex, Rightindex-leftindex + 1);
    Return (hash & 2147483647)% Numpartitions;
  }


(4) Hashpartitioner default partition, determine partition by Ke's hash value
public int getpartition (K key, V value, int numreducetasks) {
    return (Key.hashcode () & 2147483647)% Numreducetask s;
  }


Second, custom Paritioner
Key is ID, implementation is partitioned by the last three bits of key
public class Rtfpartitioner extends Partitioner<text, text> {
  @Override public
  int getpartition (Text key, Text value, int numpartitions) {
    String k = key.tostring ();
    Int par = Integer.parseint (k.substring (K.length ()-2));
    Return par;
  }

}


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.