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