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 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      // take a snapshot of every table we have.
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  }