001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.mapreduce; 020 021import org.apache.hadoop.classification.InterfaceAudience; 022import org.apache.hadoop.classification.InterfaceStability; 023import org.apache.hadoop.conf.Configurable; 024 025/** 026 * Partitions the key space. 027 * 028 * <p><code>Partitioner</code> controls the partitioning of the keys of the 029 * intermediate map-outputs. The key (or a subset of the key) is used to derive 030 * the partition, typically by a hash function. The total number of partitions 031 * is the same as the number of reduce tasks for the job. Hence this controls 032 * which of the <code>m</code> reduce tasks the intermediate key (and hence the 033 * record) is sent for reduction.</p> 034 * 035 * <p>Note: A <code>Partitioner</code> is created only when there are multiple 036 * reducers.</p> 037 * 038 * <p>Note: If you require your Partitioner class to obtain the Job's 039 * configuration object, implement the {@link Configurable} interface.</p> 040 * 041 * @see Reducer 042 */ 043@InterfaceAudience.Public 044@InterfaceStability.Stable 045public abstract class Partitioner<KEY, VALUE> { 046 047 /** 048 * Get the partition number for a given key (hence record) given the total 049 * number of partitions i.e. number of reduce-tasks for the job. 050 * 051 * <p>Typically a hash function on a all or a subset of the key.</p> 052 * 053 * @param key the key to be partioned. 054 * @param value the entry value. 055 * @param numPartitions the total number of partitions. 056 * @return the partition number for the <code>key</code>. 057 */ 058 public abstract int getPartition(KEY key, VALUE value, int numPartitions); 059 060}