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.assertArrayEquals;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
31 import java.util.UUID;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.hadoop.conf.Configurable;
36 import org.apache.hadoop.conf.Configuration;
37 import org.apache.hadoop.fs.FSDataOutputStream;
38 import org.apache.hadoop.fs.FileStatus;
39 import org.apache.hadoop.fs.FileSystem;
40 import org.apache.hadoop.fs.Path;
41 import org.apache.hadoop.hbase.Cell;
42 import org.apache.hadoop.hbase.CellUtil;
43 import org.apache.hadoop.hbase.HBaseTestingUtility;
44 import org.apache.hadoop.hbase.HConstants;
45 import org.apache.hadoop.hbase.KeyValue;
46 import org.apache.hadoop.hbase.LargeTests;
47 import org.apache.hadoop.hbase.client.HTable;
48 import org.apache.hadoop.hbase.client.Result;
49 import org.apache.hadoop.hbase.client.ResultScanner;
50 import org.apache.hadoop.hbase.client.Scan;
51 import org.apache.hadoop.hbase.util.Bytes;
52 import org.apache.hadoop.io.Text;
53 import org.apache.hadoop.mapred.Utils.OutputFileUtils.OutputFilesFilter;
54 import org.apache.hadoop.mapreduce.Job;
55 import org.apache.hadoop.util.GenericOptionsParser;
56 import org.apache.hadoop.util.Tool;
57 import org.apache.hadoop.util.ToolRunner;
58 import org.junit.AfterClass;
59 import org.junit.BeforeClass;
60 import org.junit.Test;
61 import org.junit.experimental.categories.Category;
62
63 @Category(LargeTests.class)
64 public class TestImportTsv implements Configurable {
65
66 protected static final Log LOG = LogFactory.getLog(TestImportTsv.class);
67 protected static final String NAME = TestImportTsv.class.getSimpleName();
68 protected static HBaseTestingUtility util = new HBaseTestingUtility();
69
70
71
72
73
74 protected static final String DELETE_AFTER_LOAD_CONF = NAME + ".deleteAfterLoad";
75
76
77
78
79 protected static final String FORCE_COMBINER_CONF = NAME + ".forceCombiner";
80
81 private final String FAMILY = "FAM";
82
83 public Configuration getConf() {
84 return util.getConfiguration();
85 }
86
87 public void setConf(Configuration conf) {
88 throw new IllegalArgumentException("setConf not supported");
89 }
90
91 @BeforeClass
92 public static void provisionCluster() throws Exception {
93 util.startMiniCluster();
94 util.startMiniMapReduceCluster();
95 }
96
97 @AfterClass
98 public static void releaseCluster() throws Exception {
99 util.shutdownMiniMapReduceCluster();
100 util.shutdownMiniCluster();
101 }
102
103 @Test
104 public void testMROnTable() throws Exception {
105 String table = "test-" + UUID.randomUUID();
106
107
108 String[] args = new String[] {
109 "-D" + ImportTsv.COLUMNS_CONF_KEY + "=HBASE_ROW_KEY,FAM:A,FAM:B",
110 "-D" + ImportTsv.SEPARATOR_CONF_KEY + "=\u001b",
111 table
112 };
113
114 util.createTable(table, FAMILY);
115 doMROnTableTest(util, FAMILY, null, args, 1);
116 util.deleteTable(table);
117 }
118
119 @Test
120 public void testMROnTableWithTimestamp() throws Exception {
121 String table = "test-" + UUID.randomUUID();
122
123
124 String[] args = new String[] {
125 "-D" + ImportTsv.COLUMNS_CONF_KEY
126 + "=HBASE_ROW_KEY,HBASE_TS_KEY,FAM:A,FAM:B",
127 "-D" + ImportTsv.SEPARATOR_CONF_KEY + "=,",
128 table
129 };
130 String data = "KEY,1234,VALUE1,VALUE2\n";
131
132 util.createTable(table, FAMILY);
133 doMROnTableTest(util, FAMILY, data, args, 1);
134 util.deleteTable(table);
135 }
136
137
138 @Test
139 public void testMROnTableWithCustomMapper()
140 throws Exception {
141 String table = "test-" + UUID.randomUUID();
142
143
144 String[] args = new String[] {
145 "-D" + ImportTsv.MAPPER_CONF_KEY + "=org.apache.hadoop.hbase.mapreduce.TsvImporterCustomTestMapper",
146 table
147 };
148
149 util.createTable(table, FAMILY);
150 doMROnTableTest(util, FAMILY, null, args, 3);
151 util.deleteTable(table);
152 }
153
154 @Test
155 public void testBulkOutputWithoutAnExistingTable() throws Exception {
156 String table = "test-" + UUID.randomUUID();
157
158
159 Path hfiles = new Path(util.getDataTestDirOnTestFS(table), "hfiles");
160 String[] args = new String[] {
161 "-D" + ImportTsv.COLUMNS_CONF_KEY + "=HBASE_ROW_KEY,FAM:A,FAM:B",
162 "-D" + ImportTsv.SEPARATOR_CONF_KEY + "=\u001b",
163 "-D" + ImportTsv.BULK_OUTPUT_CONF_KEY + "=" + hfiles.toString(),
164 table
165 };
166
167 doMROnTableTest(util, FAMILY, null, args, 3);
168 util.deleteTable(table);
169 }
170
171 @Test
172 public void testBulkOutputWithAnExistingTable() throws Exception {
173 String table = "test-" + UUID.randomUUID();
174
175
176 Path hfiles = new Path(util.getDataTestDirOnTestFS(table), "hfiles");
177 String[] args = new String[] {
178 "-D" + ImportTsv.COLUMNS_CONF_KEY + "=HBASE_ROW_KEY,FAM:A,FAM:B",
179 "-D" + ImportTsv.SEPARATOR_CONF_KEY + "=\u001b",
180 "-D" + ImportTsv.BULK_OUTPUT_CONF_KEY + "=" + hfiles.toString(),
181 table
182 };
183
184 util.createTable(table, FAMILY);
185 doMROnTableTest(util, FAMILY, null, args, 3);
186 util.deleteTable(table);
187 }
188
189 @Test
190 public void testJobConfigurationsWithTsvImporterTextMapper() throws Exception {
191 String table = "test-" + UUID.randomUUID();
192 Path bulkOutputPath = new Path(util.getDataTestDirOnTestFS(table),"hfiles");
193 String INPUT_FILE = "InputFile1.csv";
194
195 String[] args =
196 new String[] {
197 "-D" + ImportTsv.MAPPER_CONF_KEY
198 + "=org.apache.hadoop.hbase.mapreduce.TsvImporterTextMapper",
199 "-D" + ImportTsv.COLUMNS_CONF_KEY
200 + "=HBASE_ROW_KEY,FAM:A,FAM:B",
201 "-D" + ImportTsv.SEPARATOR_CONF_KEY + "=,",
202 "-D" + ImportTsv.BULK_OUTPUT_CONF_KEY + "=" + bulkOutputPath.toString(), table,
203 INPUT_FILE
204 };
205 GenericOptionsParser opts = new GenericOptionsParser(util.getConfiguration(), args);
206 args = opts.getRemainingArgs();
207 Job job = ImportTsv.createSubmittableJob(util.getConfiguration(), args);
208 assertTrue(job.getMapperClass().equals(TsvImporterTextMapper.class));
209 assertTrue(job.getReducerClass().equals(TextSortReducer.class));
210 assertTrue(job.getMapOutputValueClass().equals(Text.class));
211 }
212
213 @Test
214 public void testBulkOutputWithTsvImporterTextMapper() throws Exception {
215 String table = "test-" + UUID.randomUUID();
216 String FAMILY = "FAM";
217 Path bulkOutputPath = new Path(util.getDataTestDirOnTestFS(table),"hfiles");
218
219 String[] args =
220 new String[] {
221 "-D" + ImportTsv.MAPPER_CONF_KEY
222 + "=org.apache.hadoop.hbase.mapreduce.TsvImporterTextMapper",
223 "-D" + ImportTsv.COLUMNS_CONF_KEY
224 + "=HBASE_ROW_KEY,FAM:A,FAM:B",
225 "-D" + ImportTsv.SEPARATOR_CONF_KEY + "=\u001b",
226 "-D" + ImportTsv.BULK_OUTPUT_CONF_KEY + "=" + bulkOutputPath.toString(), table
227 };
228 String data = "KEY\u001bVALUE4\u001bVALUE8\n";
229 doMROnTableTest(util, FAMILY, data, args, 4);
230 }
231
232 protected static Tool doMROnTableTest(HBaseTestingUtility util, String family,
233 String data, String[] args) throws Exception {
234 return doMROnTableTest(util, family, data, args, 1);
235 }
236
237
238
239
240
241
242
243
244
245 protected static Tool doMROnTableTest(HBaseTestingUtility util, String family,
246 String data, String[] args, int valueMultiplier)
247 throws Exception {
248 String table = args[args.length - 1];
249 Configuration conf = new Configuration(util.getConfiguration());
250
251
252 FileSystem fs = FileSystem.get(conf);
253 Path inputPath = fs.makeQualified(new Path(util.getDataTestDirOnTestFS(table), "input.dat"));
254 FSDataOutputStream op = fs.create(inputPath, true);
255 if (data == null) {
256 data = "KEY\u001bVALUE1\u001bVALUE2\n";
257 }
258 op.write(Bytes.toBytes(data));
259 op.close();
260 LOG.debug(String.format("Wrote test data to file: %s", inputPath));
261
262 if (conf.getBoolean(FORCE_COMBINER_CONF, true)) {
263 LOG.debug("Forcing combiner.");
264 conf.setInt("min.num.spills.for.combine", 1);
265 }
266
267
268 List<String> argv = new ArrayList<String>(Arrays.asList(args));
269 argv.add(inputPath.toString());
270 Tool tool = new ImportTsv();
271 LOG.debug("Running ImportTsv with arguments: " + argv);
272 assertEquals(0, ToolRunner.run(conf, tool, argv.toArray(args)));
273
274
275
276
277 boolean createdHFiles = false;
278 String outputPath = null;
279 for (String arg : argv) {
280 if (arg.contains(ImportTsv.BULK_OUTPUT_CONF_KEY)) {
281 createdHFiles = true;
282
283 outputPath = arg.split("=")[1];
284 break;
285 }
286 }
287
288 if (createdHFiles)
289 validateHFiles(fs, outputPath, family);
290 else
291 validateTable(conf, table, family, valueMultiplier);
292
293 if (conf.getBoolean(DELETE_AFTER_LOAD_CONF, true)) {
294 LOG.debug("Deleting test subdirectory");
295 util.cleanupDataTestDirOnTestFS(table);
296 }
297 return tool;
298 }
299
300
301
302
303 private static void validateTable(Configuration conf, String tableName,
304 String family, int valueMultiplier) throws IOException {
305
306 LOG.debug("Validating table.");
307 HTable table = new HTable(conf, tableName);
308 boolean verified = false;
309 long pause = conf.getLong("hbase.client.pause", 5 * 1000);
310 int numRetries = conf.getInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 5);
311 for (int i = 0; i < numRetries; i++) {
312 try {
313 Scan scan = new Scan();
314
315 scan.addFamily(Bytes.toBytes(family));
316 ResultScanner resScanner = table.getScanner(scan);
317 for (Result res : resScanner) {
318 assertTrue(res.size() == 2);
319 List<Cell> kvs = res.listCells();
320 assertTrue(CellUtil.matchingRow(kvs.get(0), Bytes.toBytes("KEY")));
321 assertTrue(CellUtil.matchingRow(kvs.get(1), Bytes.toBytes("KEY")));
322 assertTrue(CellUtil.matchingValue(kvs.get(0), Bytes.toBytes("VALUE" + valueMultiplier)));
323 assertTrue(CellUtil.matchingValue(kvs.get(1), Bytes.toBytes("VALUE" + 2 * valueMultiplier)));
324
325 }
326 verified = true;
327 break;
328 } catch (NullPointerException e) {
329
330
331 }
332 try {
333 Thread.sleep(pause);
334 } catch (InterruptedException e) {
335
336 }
337 }
338 table.close();
339 assertTrue(verified);
340 }
341
342
343
344
345 private static void validateHFiles(FileSystem fs, String outputPath, String family)
346 throws IOException {
347
348
349 LOG.debug("Validating HFiles.");
350 Set<String> configFamilies = new HashSet<String>();
351 configFamilies.add(family);
352 Set<String> foundFamilies = new HashSet<String>();
353 for (FileStatus cfStatus : fs.listStatus(new Path(outputPath), new OutputFilesFilter())) {
354 String[] elements = cfStatus.getPath().toString().split(Path.SEPARATOR);
355 String cf = elements[elements.length - 1];
356 foundFamilies.add(cf);
357 assertTrue(
358 String.format(
359 "HFile ouput contains a column family (%s) not present in input families (%s)",
360 cf, configFamilies),
361 configFamilies.contains(cf));
362 for (FileStatus hfile : fs.listStatus(cfStatus.getPath())) {
363 assertTrue(
364 String.format("HFile %s appears to contain no data.", hfile.getPath()),
365 hfile.getLen() > 0);
366 }
367 }
368 }
369 }
370