1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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
73
74 @AfterClass
75 public static void tearDownAfterClass() throws Exception {
76 TEST_UTIL.shutdownMiniCluster();
77 TEST_UTIL.shutdownMiniMapReduceCluster();
78 }
79
80
81
82
83
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
95
96
97
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
109
110
111
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
123
124
125
126
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
143
144
145
146
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
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
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 }