1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | |
---|
19 | package org.apache.hadoop.mapred.pipes; |
---|
20 | |
---|
21 | import org.apache.hadoop.io.Writable; |
---|
22 | import org.apache.hadoop.io.WritableComparable; |
---|
23 | import org.apache.hadoop.mapred.JobConf; |
---|
24 | import org.apache.hadoop.mapred.Partitioner; |
---|
25 | import org.apache.hadoop.util.ReflectionUtils; |
---|
26 | |
---|
27 | /** |
---|
28 | * This partitioner is one that can either be set manually per a record or it |
---|
29 | * can fall back onto a Java partitioner that was set by the user. |
---|
30 | */ |
---|
31 | class PipesPartitioner<K extends WritableComparable, |
---|
32 | V extends Writable> |
---|
33 | implements Partitioner<K, V> { |
---|
34 | |
---|
35 | private static ThreadLocal<Integer> cache = new ThreadLocal<Integer>(); |
---|
36 | private Partitioner<K, V> part = null; |
---|
37 | |
---|
38 | @SuppressWarnings("unchecked") |
---|
39 | public void configure(JobConf conf) { |
---|
40 | part = |
---|
41 | ReflectionUtils.newInstance(Submitter.getJavaPartitioner(conf), conf); |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | * Set the next key to have the given partition. |
---|
46 | * @param newValue the next partition value |
---|
47 | */ |
---|
48 | static void setNextPartition(int newValue) { |
---|
49 | cache.set(newValue); |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * If a partition result was set manually, return it. Otherwise, we call |
---|
54 | * the Java partitioner. |
---|
55 | * @param key the key to partition |
---|
56 | * @param value the value to partition |
---|
57 | * @param numPartitions the number of reduces |
---|
58 | */ |
---|
59 | public int getPartition(K key, V value, |
---|
60 | int numPartitions) { |
---|
61 | Integer result = cache.get(); |
---|
62 | if (result == null) { |
---|
63 | return part.getPartition(key, value, numPartitions); |
---|
64 | } else { |
---|
65 | return result; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | } |
---|