View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.mapreduce;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  import java.util.ArrayList;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.MediumTests;
32  import org.apache.hadoop.hbase.client.HTable;
33  import org.apache.hadoop.hbase.client.Put;
34  import org.apache.hadoop.hbase.mapreduce.RowCounter.RowCounterMapper;
35  import org.apache.hadoop.hbase.util.Bytes;
36  import org.apache.hadoop.mapreduce.Counter;
37  import org.apache.hadoop.mapreduce.Job;
38  import org.apache.hadoop.util.GenericOptionsParser;
39  import org.junit.AfterClass;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  /**
45   * Test the rowcounter map reduce job.
46   */
47  @Category(MediumTests.class)
48  public class TestRowCounter {
49    final Log LOG = LogFactory.getLog(getClass());
50    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
51    private final static String TABLE_NAME = "testRowCounter";
52    private final static String COL_FAM = "col_fam";
53    private final static String COL1 = "c1";
54    private final static String COL2 = "c2";
55    private final static int TOTAL_ROWS = 10;
56    private final static int ROWS_WITH_ONE_COL = 2;
57  
58    /**
59     * @throws java.lang.Exception
60     */
61    @BeforeClass
62    public static void setUpBeforeClass() throws Exception {
63      TEST_UTIL.startMiniCluster();
64      TEST_UTIL.startMiniMapReduceCluster();
65      HTable table = TEST_UTIL.createTable(Bytes.toBytes(TABLE_NAME),
66          Bytes.toBytes(COL_FAM));
67      writeRows(table);
68      table.close();
69    }
70  
71    /**
72     * @throws java.lang.Exception
73     */
74    @AfterClass
75    public static void tearDownAfterClass() throws Exception {
76      TEST_UTIL.shutdownMiniCluster();
77      TEST_UTIL.shutdownMiniMapReduceCluster();
78    }
79  
80    /**
81     * Test a case when no column was specified in command line arguments.
82     * 
83     * @throws Exception
84     */
85    @Test
86    public void testRowCounterNoColumn() throws Exception {
87      String[] args = new String[] {
88          TABLE_NAME
89      };
90      runRowCount(args, 10);
91    }
92  
93    /**
94     * Test a case when the column specified in command line arguments is
95     * exclusive for few rows.
96     * 
97     * @throws Exception
98     */
99    @Test
100   public void testRowCounterExclusiveColumn() throws Exception {
101     String[] args = new String[] {
102         TABLE_NAME, COL_FAM + ":" + COL1
103     };
104     runRowCount(args, 8);
105   }
106 
107   /**
108    * Test a case when the column specified in command line arguments is not part
109    * of first KV for a row.
110    * 
111    * @throws Exception
112    */
113   @Test
114   public void testRowCounterHiddenColumn() throws Exception {
115     String[] args = new String[] {
116         TABLE_NAME, COL_FAM + ":" + COL2
117     };
118     runRowCount(args, 10);
119   }
120 
121   /**
122    * Run the RowCounter map reduce job and verify the row count.
123    * 
124    * @param args the command line arguments to be used for rowcounter job.
125    * @param expectedCount the expected row count (result of map reduce job).
126    * @throws Exception
127    */
128   private void runRowCount(String[] args, int expectedCount) throws Exception {
129     GenericOptionsParser opts = new GenericOptionsParser(
130         TEST_UTIL.getConfiguration(), args);
131     Configuration conf = opts.getConfiguration();
132     args = opts.getRemainingArgs();
133     Job job = RowCounter.createSubmittableJob(conf, args);
134     job.waitForCompletion(true);
135     assertTrue(job.isSuccessful());
136     Counter counter = job.getCounters().findCounter(
137         RowCounterMapper.Counters.ROWS);
138     assertEquals(expectedCount, counter.getValue());
139   }
140 
141   /**
142    * Writes TOTAL_ROWS number of distinct rows in to the table. Few rows have
143    * two columns, Few have one.
144    * 
145    * @param table
146    * @throws IOException
147    */
148   private static void writeRows(HTable table) throws IOException {
149     final byte[] family = Bytes.toBytes(COL_FAM);
150     final byte[] value = Bytes.toBytes("abcd");
151     final byte[] col1 = Bytes.toBytes(COL1);
152     final byte[] col2 = Bytes.toBytes(COL2);
153     ArrayList<Put> rowsUpdate = new ArrayList<Put>();
154     // write few rows with two columns
155     int i = 0;
156     for (; i < TOTAL_ROWS - ROWS_WITH_ONE_COL; i++) {
157       byte[] row = Bytes.toBytes("row" + i);
158       Put put = new Put(row);
159       put.add(family, col1, value);
160       put.add(family, col2, value);
161       rowsUpdate.add(put);
162     }
163 
164     // write few rows with only one column
165     for (; i < TOTAL_ROWS; i++) {
166       byte[] row = Bytes.toBytes("row" + i);
167       Put put = new Put(row);
168       put.add(family, col2, value);
169       rowsUpdate.add(put);
170     }
171     table.put(rowsUpdate);
172   }
173 }