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