View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.mapreduce;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.*;
25  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
26  import org.apache.hadoop.hbase.testclassification.SmallTests;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.junit.experimental.categories.Category;
29  
30  import org.junit.Test;
31  
32  /**
33   * Test of simple partitioner.
34   */
35  @Category(SmallTests.class)
36  public class TestSimpleTotalOrderPartitioner {
37    protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
38    Configuration conf = TEST_UTIL.getConfiguration();
39    
40    @Test
41    public void testSplit() throws Exception {
42      String start = "a";
43      String end = "{";
44      SimpleTotalOrderPartitioner<byte []> p =
45        new SimpleTotalOrderPartitioner<byte []>();
46      
47      this.conf.set(SimpleTotalOrderPartitioner.START, start);
48      this.conf.set(SimpleTotalOrderPartitioner.END, end);
49      p.setConf(this.conf);
50      ImmutableBytesWritable c = new ImmutableBytesWritable(Bytes.toBytes("c"));
51      // If one reduce, partition should be 0.
52      int partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 1);
53      assertEquals(0, partition);
54      // If two reduces, partition should be 0.
55      partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 2);
56      assertEquals(0, partition);
57      // Divide in 3.
58      partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 3);
59      assertEquals(0, partition);
60      ImmutableBytesWritable q = new ImmutableBytesWritable(Bytes.toBytes("q"));
61      partition = p.getPartition(q, HConstants.EMPTY_BYTE_ARRAY, 2);
62      assertEquals(1, partition);
63      partition = p.getPartition(q, HConstants.EMPTY_BYTE_ARRAY, 3);
64      assertEquals(2, partition);
65      // What about end and start keys.
66      ImmutableBytesWritable startBytes =
67        new ImmutableBytesWritable(Bytes.toBytes(start));
68      partition = p.getPartition(startBytes, HConstants.EMPTY_BYTE_ARRAY, 2);
69      assertEquals(0, partition);
70      partition = p.getPartition(startBytes, HConstants.EMPTY_BYTE_ARRAY, 3);
71      assertEquals(0, partition);
72      ImmutableBytesWritable endBytes =
73        new ImmutableBytesWritable(Bytes.toBytes("z"));
74      partition = p.getPartition(endBytes, HConstants.EMPTY_BYTE_ARRAY, 2);
75      assertEquals(1, partition);
76      partition = p.getPartition(endBytes, HConstants.EMPTY_BYTE_ARRAY, 3);
77      assertEquals(2, partition);
78    }
79  
80  }
81