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.client;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.MediumTests;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.junit.AfterClass;
32  import org.junit.BeforeClass;
33  import org.junit.Test;
34  import org.junit.experimental.categories.Category;
35  
36  /**
37   * This class provides tests for the {@link HTableUtil} class
38   *
39   */
40  @Category(MediumTests.class)
41  public class TestHTableUtil {
42    final Log LOG = LogFactory.getLog(getClass());
43    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
44    private static byte [] FAMILY = Bytes.toBytes("testFamily");
45    private static byte [] QUALIFIER = Bytes.toBytes("testQualifier");
46    private static byte [] VALUE = Bytes.toBytes("testValue");
47  
48    /**
49     * @throws java.lang.Exception
50     */
51    @BeforeClass
52    public static void setUpBeforeClass() throws Exception {
53      TEST_UTIL.startMiniCluster();
54    }
55  
56    /**
57     * @throws java.lang.Exception
58     */
59    @AfterClass
60    public static void tearDownAfterClass() throws Exception {
61      TEST_UTIL.shutdownMiniCluster();
62    }
63    
64    /**
65     *
66     * @throws Exception
67     */
68    @Test
69    public void testBucketPut() throws Exception {
70      byte [] TABLE = Bytes.toBytes("testBucketPut");
71      HTable ht = TEST_UTIL.createTable(TABLE, FAMILY);
72      ht.setAutoFlush(false, true);
73  
74      List<Put> puts = new ArrayList<Put>();
75      puts.add( createPut("row1") );
76      puts.add( createPut("row2") );
77      puts.add( createPut("row3") );
78      puts.add( createPut("row4") );
79      
80      HTableUtil.bucketRsPut( ht, puts );
81      
82      Scan scan = new Scan();
83      scan.addColumn(FAMILY, QUALIFIER);
84      int count = 0;
85      for(Result result : ht.getScanner(scan)) {
86        count++;
87      }
88      LOG.info("bucket put count=" + count);
89      assertEquals(count, puts.size());
90      ht.close();
91     }
92  
93    private Put createPut(String row) {
94      Put put = new Put( Bytes.toBytes(row));
95      put.add(FAMILY, QUALIFIER, VALUE);
96      return put;
97    }
98    
99    /**
100   *
101   * @throws Exception
102   */
103  @Test
104  public void testBucketBatch() throws Exception {
105    byte [] TABLE = Bytes.toBytes("testBucketBatch");
106    HTable ht = TEST_UTIL.createTable(TABLE, FAMILY);
107 
108    List<Row> rows = new ArrayList<Row>();
109    rows.add( createPut("row1") );
110    rows.add( createPut("row2") );
111    rows.add( createPut("row3") );
112    rows.add( createPut("row4") );
113    
114    HTableUtil.bucketRsBatch( ht, rows );
115    
116    Scan scan = new Scan();
117    scan.addColumn(FAMILY, QUALIFIER);
118    
119    int count = 0;
120    for(Result result : ht.getScanner(scan)) {
121      count++;
122    }
123    LOG.info("bucket batch count=" + count);
124    assertEquals(count, rows.size());
125    ht.close();
126  }
127 
128 
129 }
130