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