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 */
018package org.apache.hadoop.hbase.mapreduce;
019
020import static org.junit.Assert.assertEquals;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
027import org.apache.hadoop.hbase.testclassification.MapReduceTests;
028import org.apache.hadoop.hbase.testclassification.MediumTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.AfterClass;
031import org.junit.BeforeClass;
032import org.junit.ClassRule;
033import org.junit.Rule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.junit.rules.TestName;
037
038@Category({ MapReduceTests.class, MediumTests.class })
039public class TestHRegionPartitioner {
040
041  @ClassRule
042  public static final HBaseClassTestRule CLASS_RULE =
043    HBaseClassTestRule.forClass(TestHRegionPartitioner.class);
044
045  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
046
047  @Rule
048  public TestName name = new TestName();
049
050  @BeforeClass
051  public static void beforeClass() throws Exception {
052    UTIL.startMiniCluster();
053  }
054
055  @AfterClass
056  public static void afterClass() throws Exception {
057    UTIL.shutdownMiniCluster();
058  }
059
060  /**
061   * Test HRegionPartitioner
062   */
063  @Test
064  public void testHRegionPartitioner() throws Exception {
065
066    byte[][] families = { Bytes.toBytes("familyA"), Bytes.toBytes("familyB") };
067
068    UTIL.createTable(TableName.valueOf(name.getMethodName()), families, 1, Bytes.toBytes("aa"),
069      Bytes.toBytes("cc"), 3);
070
071    HRegionPartitioner<Long, Long> partitioner = new HRegionPartitioner<>();
072    Configuration configuration = UTIL.getConfiguration();
073    configuration.set(TableOutputFormat.OUTPUT_TABLE, name.getMethodName());
074    partitioner.setConf(configuration);
075    ImmutableBytesWritable writable = new ImmutableBytesWritable(Bytes.toBytes("bb"));
076
077    assertEquals(1, partitioner.getPartition(writable, 10L, 3));
078    assertEquals(0, partitioner.getPartition(writable, 10L, 1));
079  }
080
081  @Test
082  public void testHRegionPartitionerMoreRegions() throws Exception {
083    byte[][] families = { Bytes.toBytes("familyA"), Bytes.toBytes("familyB") };
084
085    TableName tableName = TableName.valueOf(name.getMethodName());
086    UTIL.createTable(tableName, families, 1, Bytes.toBytes("aa"), Bytes.toBytes("cc"), 5);
087
088    Configuration configuration = UTIL.getConfiguration();
089    int numberOfRegions = UTIL.getMiniHBaseCluster().getRegions(tableName).size();
090    assertEquals(5, numberOfRegions);
091
092    HRegionPartitioner<Long, Long> partitioner = new HRegionPartitioner<>();
093    configuration.set(TableOutputFormat.OUTPUT_TABLE, name.getMethodName());
094    partitioner.setConf(configuration);
095
096    // Get some rowKey for the lastRegion
097    ImmutableBytesWritable writable = new ImmutableBytesWritable(Bytes.toBytes("df"));
098
099    // getPartition should return 4 since number of partition = number of reduces.
100    assertEquals(4, partitioner.getPartition(writable, 10L, 5));
101  }
102}