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 edu.umd.cs.findbugs.annotations.Nullable;
25  import org.apache.hadoop.fs.Path;
26  import org.apache.hadoop.hbase.TableName;
27  import org.apache.hadoop.hbase.client.Scan;
28  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
29  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
30  import org.apache.hadoop.hbase.testclassification.LargeTests;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.apache.hadoop.hbase.util.FSUtils;
33  import org.apache.hadoop.mapreduce.Job;
34  import org.junit.Before;
35  import org.junit.BeforeClass;
36  import org.junit.experimental.categories.Category;
37  
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 = TEST_UTIL.getRandomDir();
67    }
68  
69    @Override
70    protected void initJob(List<Scan> scans, Job job) throws IOException {
71      TableMapReduceUtil
72          .initMultiTableSnapshotMapperJob(getSnapshotScanMapping(scans), ScanMapper.class,
73              ImmutableBytesWritable.class, ImmutableBytesWritable.class, job, true, restoreDir);
74    }
75  
76    protected Map<String, Collection<Scan>> getSnapshotScanMapping(final List<Scan> scans) {
77      return Multimaps.index(scans, new Function<Scan, String>() {
78        @Nullable
79        @Override
80        public String apply(Scan input) {
81          return snapshotNameForTable(
82              Bytes.toStringBinary(input.getAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME)));
83        }
84      }).asMap();
85    }
86  
87    public static String snapshotNameForTable(String tableName) {
88      return tableName + "_snapshot";
89    }
90  
91  }