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 com.google.common.base.Function;
22 import com.google.common.collect.ImmutableList;
23 import com.google.common.collect.Multimaps;
24 import org.apache.hadoop.fs.Path;
25 import org.apache.hadoop.hbase.TableName;
26 import org.apache.hadoop.hbase.client.Scan;
27 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
28 import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
29 import org.apache.hadoop.hbase.testclassification.LargeTests;
30 import org.apache.hadoop.hbase.util.Bytes;
31 import org.apache.hadoop.hbase.util.FSUtils;
32 import org.apache.hadoop.mapreduce.Job;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.experimental.categories.Category;
36
37 import javax.annotation.Nullable;
38 import java.io.IOException;
39 import java.util.Collection;
40 import java.util.List;
41 import java.util.Map;
42
43 @Category({ LargeTests.class })
44 public class TestMultiTableSnapshotInputFormat extends MultiTableInputFormatTestBase {
45
46 protected Path restoreDir;
47
48 @BeforeClass
49 public static void setUpSnapshots() throws Exception {
50
51 TEST_UTIL.enableDebug(MultiTableSnapshotInputFormat.class);
52 TEST_UTIL.enableDebug(MultiTableSnapshotInputFormatImpl.class);
53
54
55 for (String tableName : TABLES) {
56 SnapshotTestingUtils
57 .createSnapshotAndValidate(TEST_UTIL.getHBaseAdmin(), TableName.valueOf(tableName),
58 ImmutableList.of(MultiTableInputFormatTestBase.INPUT_FAMILY), null,
59 snapshotNameForTable(tableName), FSUtils.getRootDir(TEST_UTIL.getConfiguration()),
60 TEST_UTIL.getTestFileSystem(), true);
61 }
62 }
63
64 @Before
65 public void setUp() throws Exception {
66 this.restoreDir = new Path("/tmp");
67
68 }
69
70 @Override
71 protected void initJob(List<Scan> scans, Job job) throws IOException {
72 TableMapReduceUtil
73 .initMultiTableSnapshotMapperJob(getSnapshotScanMapping(scans), ScanMapper.class,
74 ImmutableBytesWritable.class, ImmutableBytesWritable.class, job, true, restoreDir);
75 }
76
77 protected Map<String, Collection<Scan>> getSnapshotScanMapping(final List<Scan> scans) {
78 return Multimaps.index(scans, new Function<Scan, String>() {
79 @Nullable
80 @Override
81 public String apply(Scan input) {
82 return snapshotNameForTable(
83 Bytes.toStringBinary(input.getAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME)));
84 }
85 }).asMap();
86 }
87
88 public static String snapshotNameForTable(String tableName) {
89 return tableName + "_snapshot";
90 }
91
92 }