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