Partitioner in hadoop

Source: Internet
Author: User

At the beginning, people thought that only one reduce is enough for mapreduce programs. After all, before you process data, a reducer has already divided the data into good classes. Who does not like classified data. However, we ignore the advantages of parallel computing. If there is only one reducer, our cloud computing will degrade into a light rain.


When there are multiple reducers, we need a mechanism to control the allocation of mapper results. This is the work of partitioner.

By default, hadoop allocates keys by comparing their hash values. By default, hashpartitioner is used. Sometimes the default functionality does not meet our requirements, such as the edge class we previously customized (http://blog.csdn.net/on_way_/article/details/8589187 ). When we want to know the number of passengers taking off at each airport. We have the following data:

(Beijing, Shanghai) James

(Beijing, Qingdao) Li Si .......

If we use hashpartitioner for allocation, the two rows will be sent to different reducers, and the number of airport departures will be calculated twice, and each time it is wrong.

We need to customize a partitioner for our application.

[Java]View plaincopyprint?
  1. Import org. Apache. hadoop. Io. writable;
  2. Import org. Apache. hadoop. mapred. jobconf;
  3. Import org. Apache. hadoop. mapred. partitioner;
  4. Public class edgepartitioner implements partitioner <edge, writable> {
  5. @ Override
  6. Public void configure (jobconf job ){
  7. // Todo auto-generated method stub
  8. }
  9. @ Override
  10. Public int getpartition (edge key, writable value, int numpartitions ){
  11. // Todo auto-generated method stub
  12. Return key. getdeparturenode (). hashcode () % numpartitions;
  13. }
  14. }

The following figure illustrates partitioner.

Between the map and reduce stages, a mapreduce program must allocate mapper output to multiple reducers. This process is called shuffling, because the output result of a Mapper may be allocated to multiple nodes in the cluster.



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.