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.util.Bytes;
27  import org.junit.experimental.categories.Category;
28  
29  import org.junit.Test;
30  
31  /**
32   * Test of simple partitioner.
33   */
34  @Category(SmallTests.class)
35  public class TestSimpleTotalOrderPartitioner {
36    protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
37    Configuration conf = TEST_UTIL.getConfiguration();
38    
39    @Test
40    public void testSplit() throws Exception {
41      String start = "a";
42      String end = "{";
43      SimpleTotalOrderPartitioner<byte []> p =
44        new SimpleTotalOrderPartitioner<byte []>();
45      
46      this.conf.set(SimpleTotalOrderPartitioner.START, start);
47      this.conf.set(SimpleTotalOrderPartitioner.END, end);
48      p.setConf(this.conf);
49      ImmutableBytesWritable c = new ImmutableBytesWritable(Bytes.toBytes("c"));
50      // If one reduce, partition should be 0.
51      int partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 1);
52      assertEquals(0, partition);
53      // If two reduces, partition should be 0.
54      partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 2);
55      assertEquals(0, partition);
56      // Divide in 3.
57      partition = p.getPartition(c, HConstants.EMPTY_BYTE_ARRAY, 3);
58      assertEquals(0, partition);
59      ImmutableBytesWritable q = new ImmutableBytesWritable(Bytes.toBytes("q"));
60      partition = p.getPartition(q, HConstants.EMPTY_BYTE_ARRAY, 2);
61      assertEquals(1, partition);
62      partition = p.getPartition(q, HConstants.EMPTY_BYTE_ARRAY, 3);
63      assertEquals(2, partition);
64      // What about end and start keys.
65      ImmutableBytesWritable startBytes =
66        new ImmutableBytesWritable(Bytes.toBytes(start));
67      partition = p.getPartition(startBytes, HConstants.EMPTY_BYTE_ARRAY, 2);
68      assertEquals(0, partition);
69      partition = p.getPartition(startBytes, HConstants.EMPTY_BYTE_ARRAY, 3);
70      assertEquals(0, partition);
71      ImmutableBytesWritable endBytes =
72        new ImmutableBytesWritable(Bytes.toBytes("z"));
73      partition = p.getPartition(endBytes, HConstants.EMPTY_BYTE_ARRAY, 2);
74      assertEquals(1, partition);
75      partition = p.getPartition(endBytes, HConstants.EMPTY_BYTE_ARRAY, 3);
76      assertEquals(2, partition);
77    }
78  
79  }
80