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
019 package org.apache.hadoop.mapreduce;
020
021 import org.apache.hadoop.classification.InterfaceAudience;
022 import org.apache.hadoop.classification.InterfaceStability;
023 import 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 * Note: If you require your Partitioner class to obtain the Job's configuration
036 * object, implement the {@link Configurable} interface.
037 *
038 * @see Reducer
039 */
040 @InterfaceAudience.Public
041 @InterfaceStability.Stable
042 public abstract class Partitioner<KEY, VALUE> {
043
044 /**
045 * Get the partition number for a given key (hence record) given the total
046 * number of partitions i.e. number of reduce-tasks for the job.
047 *
048 * <p>Typically a hash function on a all or a subset of the key.</p>
049 *
050 * @param key the key to be partioned.
051 * @param value the entry value.
052 * @param numPartitions the total number of partitions.
053 * @return the partition number for the <code>key</code>.
054 */
055 public abstract int getPartition(KEY key, VALUE value, int numPartitions);
056
057 }