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