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