001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.mapreduce;
020
021import org.apache.yetus.audience.InterfaceAudience;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024import org.apache.hadoop.conf.Configurable;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
027import org.apache.hadoop.hbase.util.Base64;
028import org.apache.hadoop.hbase.util.Bytes;
029import org.apache.hadoop.mapreduce.Partitioner;
030
031/**
032 * A partitioner that takes start and end keys and uses bigdecimal to figure
033 * which reduce a key belongs to.  Pass the start and end
034 * keys in the Configuration using <code>hbase.simpletotalorder.start</code>
035 * and <code>hbase.simpletotalorder.end</code>.  The end key needs to be
036 * exclusive; i.e. one larger than the biggest key in your key space.
037 * You may be surprised at how this class partitions the space; it may not
038 * align with preconceptions; e.g. a start key of zero and an end key of 100
039 * divided in ten will not make regions whose range is 0-10, 10-20, and so on.
040 * Make your own partitioner if you need the region spacing to come out a
041 * particular way.
042 * @param <VALUE>
043 * @see #START
044 * @see #END
045 */
046@InterfaceAudience.Public
047public class SimpleTotalOrderPartitioner<VALUE> extends Partitioner<ImmutableBytesWritable, VALUE>
048implements Configurable {
049  private final static Logger LOG = LoggerFactory.getLogger(SimpleTotalOrderPartitioner.class);
050
051  @Deprecated
052  public static final String START = "hbase.simpletotalorder.start";
053  @Deprecated
054  public static final String END = "hbase.simpletotalorder.end";
055
056  static final String START_BASE64 = "hbase.simpletotalorder.start.base64";
057  static final String END_BASE64 = "hbase.simpletotalorder.end.base64";
058
059  private Configuration c;
060  private byte [] startkey;
061  private byte [] endkey;
062  private byte [][] splits;
063  private int lastReduces = -1;
064
065  public static void setStartKey(Configuration conf, byte[] startKey) {
066    conf.set(START_BASE64, Base64.encodeBytes(startKey));
067  }
068
069  public static void setEndKey(Configuration conf, byte[] endKey) {
070    conf.set(END_BASE64, Base64.encodeBytes(endKey));
071  }
072
073  @SuppressWarnings("deprecation")
074  static byte[] getStartKey(Configuration conf) {
075    return getKeyFromConf(conf, START_BASE64, START);
076  }
077
078  @SuppressWarnings("deprecation")
079  static byte[] getEndKey(Configuration conf) {
080    return getKeyFromConf(conf, END_BASE64, END);
081  }
082
083  private static byte[] getKeyFromConf(Configuration conf,
084      String base64Key, String deprecatedKey) {
085    String encoded = conf.get(base64Key);
086    if (encoded != null) {
087      return Base64.decode(encoded);
088    }
089    String oldStyleVal = conf.get(deprecatedKey);
090    if (oldStyleVal == null) {
091      return null;
092    }
093    LOG.warn("Using deprecated configuration " + deprecatedKey +
094        " - please use static accessor methods instead.");
095    return Bytes.toBytesBinary(oldStyleVal);
096  }
097
098  @Override
099  public int getPartition(final ImmutableBytesWritable key, final VALUE value,
100      final int reduces) {
101    if (reduces == 1) return 0;
102    if (this.lastReduces != reduces) {
103      this.splits = Bytes.split(this.startkey, this.endkey, reduces - 1);
104      for (int i = 0; i < splits.length; i++) {
105        LOG.info(Bytes.toStringBinary(splits[i]));
106      }
107      this.lastReduces = reduces;
108    }
109    int pos = Bytes.binarySearch(this.splits, key.get(), key.getOffset(),
110      key.getLength());
111    // Below code is from hfile index search.
112    if (pos < 0) {
113      pos++;
114      pos *= -1;
115      if (pos == 0) {
116        // falls before the beginning of the file.
117        throw new RuntimeException("Key outside start/stop range: " +
118          key.toString());
119      }
120      pos--;
121    }
122    return pos;
123  }
124
125  @Override
126  public Configuration getConf() {
127    return this.c;
128  }
129
130  @Override
131  public void setConf(Configuration conf) {
132    this.c = conf;
133    this.startkey = getStartKey(conf);
134    this.endkey = getEndKey(conf);
135    if (startkey == null || endkey == null) {
136      throw new RuntimeException(this.getClass() + " not configured");
137    }
138    LOG.info("startkey=" + Bytes.toStringBinary(startkey) +
139        ", endkey=" + Bytes.toStringBinary(endkey));
140    // Reset last reduces count on change of Start / End key
141    this.lastReduces = -1;
142  }
143}