001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.mapreduce;
019
020import edu.umd.cs.findbugs.annotations.Nullable;
021import java.io.IOException;
022import java.util.Collection;
023import java.util.List;
024import java.util.Map;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.Scan;
029import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
030import org.apache.hadoop.hbase.logging.Log4jUtils;
031import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
032import org.apache.hadoop.hbase.testclassification.LargeTests;
033import org.apache.hadoop.hbase.testclassification.VerySlowMapReduceTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.hadoop.hbase.util.CommonFSUtils;
036import org.apache.hadoop.mapreduce.Job;
037import org.junit.Before;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.experimental.categories.Category;
041
042import org.apache.hbase.thirdparty.com.google.common.base.Function;
043import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
044import org.apache.hbase.thirdparty.com.google.common.collect.Multimaps;
045
046@Category({ VerySlowMapReduceTests.class, LargeTests.class })
047public class TestMultiTableSnapshotInputFormat extends MultiTableInputFormatTestBase {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestMultiTableSnapshotInputFormat.class);
052
053  protected Path restoreDir;
054
055  @BeforeClass
056  public static void setUpSnapshots() throws Exception {
057    Log4jUtils.enableDebug(MultiTableSnapshotInputFormat.class);
058    Log4jUtils.enableDebug(MultiTableSnapshotInputFormatImpl.class);
059
060    // take a snapshot of every table we have.
061    for (String tableName : TABLES) {
062      SnapshotTestingUtils.createSnapshotAndValidate(TEST_UTIL.getAdmin(),
063        TableName.valueOf(tableName), ImmutableList.of(INPUT_FAMILY), null,
064        snapshotNameForTable(tableName), CommonFSUtils.getRootDir(TEST_UTIL.getConfiguration()),
065        TEST_UTIL.getTestFileSystem(), true);
066    }
067  }
068
069  @Before
070  public void setUp() throws Exception {
071    this.restoreDir = TEST_UTIL.getRandomDir();
072  }
073
074  @Override
075  protected void initJob(List<Scan> scans, Job job) throws IOException {
076    TableMapReduceUtil.initMultiTableSnapshotMapperJob(getSnapshotScanMapping(scans),
077      ScanMapper.class, ImmutableBytesWritable.class, ImmutableBytesWritable.class, job, true,
078      restoreDir);
079  }
080
081  protected Map<String, Collection<Scan>> getSnapshotScanMapping(final List<Scan> scans) {
082    return Multimaps.index(scans, new Function<Scan, String>() {
083      @Nullable
084      @Override
085      public String apply(Scan input) {
086        return snapshotNameForTable(
087          Bytes.toStringBinary(input.getAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME)));
088      }
089    }).asMap();
090  }
091
092  public static String snapshotNameForTable(String tableName) {
093    return tableName + "_snapshot";
094  }
095
096}