1   /*
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.client;
21  
22  import static org.junit.Assert.assertEquals;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.MediumTests;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.junit.AfterClass;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  /**
38   * This class provides tests for the {@link HTableUtil} class
39   *
40   */
41  @Category(MediumTests.class)
42  public class TestHTableUtil {
43    final Log LOG = LogFactory.getLog(getClass());
44    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
45    private static byte [] ROW = Bytes.toBytes("testRow");
46    private static byte [] FAMILY = Bytes.toBytes("testFamily");
47    private static byte [] QUALIFIER = Bytes.toBytes("testQualifier");
48    private static byte [] VALUE = Bytes.toBytes("testValue");
49  
50    /**
51     * @throws java.lang.Exception
52     */
53    @BeforeClass
54    public static void setUpBeforeClass() throws Exception {
55      TEST_UTIL.startMiniCluster();
56    }
57  
58    /**
59     * @throws java.lang.Exception
60     */
61    @AfterClass
62    public static void tearDownAfterClass() throws Exception {
63      TEST_UTIL.shutdownMiniCluster();
64    }
65    
66    /**
67     *
68     * @throws Exception
69     */
70    @Test
71    public void testBucketPut() throws Exception {
72      byte [] TABLE = Bytes.toBytes("testBucketPut");
73      HTable ht = TEST_UTIL.createTable(TABLE, FAMILY);
74      ht.setAutoFlush( false );
75      
76      List<Put> puts = new ArrayList<Put>();
77      puts.add( createPut("row1") );
78      puts.add( createPut("row2") );
79      puts.add( createPut("row3") );
80      puts.add( createPut("row4") );
81      
82      HTableUtil.bucketRsPut( ht, puts );
83      
84      Scan scan = new Scan();
85      scan.addColumn(FAMILY, QUALIFIER);
86      int count = 0;
87      for(Result result : ht.getScanner(scan)) {
88        count++;
89      }
90      LOG.info("bucket put count=" + count);
91      assertEquals(count, puts.size());
92      ht.close();
93     }
94  
95    private Put createPut(String row) {
96      Put put = new Put( Bytes.toBytes(row));
97      put.add(FAMILY, QUALIFIER, VALUE);
98      return put;
99    }
100   
101   /**
102   *
103   * @throws Exception
104   */
105  @Test
106  public void testBucketBatch() throws Exception {
107    byte [] TABLE = Bytes.toBytes("testBucketBatch");
108    HTable ht = TEST_UTIL.createTable(TABLE, FAMILY);
109 
110    List<Row> rows = new ArrayList<Row>();
111    rows.add( createPut("row1") );
112    rows.add( createPut("row2") );
113    rows.add( createPut("row3") );
114    rows.add( createPut("row4") );
115    
116    HTableUtil.bucketRsBatch( ht, rows );
117    
118    Scan scan = new Scan();
119    scan.addColumn(FAMILY, QUALIFIER);
120    
121    int count = 0;
122    for(Result result : ht.getScanner(scan)) {
123      count++;
124    }
125    LOG.info("bucket batch count=" + count);
126    assertEquals(count, rows.size());
127    ht.close();
128  }
129 
130 
131   @org.junit.Rule
132   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
133     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
134 }
135