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.HConstants; 026import org.apache.hadoop.hbase.io.ImmutableBytesWritable; 027import org.apache.hadoop.hbase.testclassification.MapReduceTests; 028import org.apache.hadoop.hbase.testclassification.SmallTests; 029import org.apache.hadoop.hbase.util.Bytes; 030import org.junit.ClassRule; 031import org.junit.Test; 032import org.junit.experimental.categories.Category; 033 034/** 035 * Test of simple partitioner. 036 */ 037@Category({ MapReduceTests.class, SmallTests.class }) 038public class TestSimpleTotalOrderPartitioner { 039 040 @ClassRule 041 public static final HBaseClassTestRule CLASS_RULE = 042 HBaseClassTestRule.forClass(TestSimpleTotalOrderPartitioner.class); 043 044 protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 045 Configuration conf = TEST_UTIL.getConfiguration(); 046 047 @Test 048 public void testSplit() throws Exception { 049 String start = "a"; 050 String end = "{"; 051 SimpleTotalOrderPartitioner<byte[]> p = new SimpleTotalOrderPartitioner<>(); 052 053 this.conf.set(SimpleTotalOrderPartitioner.START, start); 054 this.conf.set(SimpleTotalOrderPartitioner.END, end); 055 p.setConf(this.conf); 056 ImmutableBytesWritable c = new ImmutableBytesWritable(Bytes.toBytes("c")); 057 // If one reduce, partition should be 0. 058 int partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 1); 059 assertEquals(0, partition); 060 // If two reduces, partition should be 0. 061 partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 2); 062 assertEquals(0, partition); 063 // Divide in 3. 064 partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 3); 065 assertEquals(0, partition); 066 ImmutableBytesWritable q = new ImmutableBytesWritable(Bytes.toBytes("q")); 067 partition = p.getPartition(q, HConstants.EMPTY_BYTE_ARRAY, 2); 068 assertEquals(1, partition); 069 partition = p.getPartition(q, HConstants.EMPTY_BYTE_ARRAY, 3); 070 assertEquals(2, partition); 071 // What about end and start keys. 072 ImmutableBytesWritable startBytes = new ImmutableBytesWritable(Bytes.toBytes(start)); 073 partition = p.getPartition(startBytes, HConstants.EMPTY_BYTE_ARRAY, 2); 074 assertEquals(0, partition); 075 partition = p.getPartition(startBytes, HConstants.EMPTY_BYTE_ARRAY, 3); 076 assertEquals(0, partition); 077 ImmutableBytesWritable endBytes = new ImmutableBytesWritable(Bytes.toBytes("z")); 078 partition = p.getPartition(endBytes, HConstants.EMPTY_BYTE_ARRAY, 2); 079 assertEquals(1, partition); 080 partition = p.getPartition(endBytes, HConstants.EMPTY_BYTE_ARRAY, 3); 081 assertEquals(2, partition); 082 } 083 084}