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.snapshot.SnapshotTestingUtils;
031import org.apache.hadoop.hbase.testclassification.LargeTests;
032import org.apache.hadoop.hbase.testclassification.VerySlowMapReduceTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.apache.hadoop.hbase.util.CommonFSUtils;
035import org.apache.hadoop.mapreduce.Job;
036import org.junit.Before;
037import org.junit.BeforeClass;
038import org.junit.ClassRule;
039import org.junit.experimental.categories.Category;
040
041import org.apache.hbase.thirdparty.com.google.common.base.Function;
042import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
043import org.apache.hbase.thirdparty.com.google.common.collect.Multimaps;
044
045@Category({ VerySlowMapReduceTests.class, LargeTests.class })
046public class TestMultiTableSnapshotInputFormat extends MultiTableInputFormatTestBase {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestMultiTableSnapshotInputFormat.class);
051
052  protected Path restoreDir;
053
054  @BeforeClass
055  public static void setUpSnapshots() throws Exception {
056    TEST_UTIL.enableDebug(MultiTableSnapshotInputFormat.class);
057    TEST_UTIL.enableDebug(MultiTableSnapshotInputFormatImpl.class);
058
059    // take a snapshot of every table we have.
060    for (String tableName : TABLES) {
061      SnapshotTestingUtils.createSnapshotAndValidate(TEST_UTIL.getAdmin(),
062        TableName.valueOf(tableName), ImmutableList.of(INPUT_FAMILY), null,
063        snapshotNameForTable(tableName), CommonFSUtils.getRootDir(TEST_UTIL.getConfiguration()),
064        TEST_UTIL.getTestFileSystem(), true);
065    }
066  }
067
068  @Before
069  public void setUp() throws Exception {
070    this.restoreDir = TEST_UTIL.getRandomDir();
071  }
072
073  @Override
074  protected void initJob(List<Scan> scans, Job job) throws IOException {
075    TableMapReduceUtil.initMultiTableSnapshotMapperJob(getSnapshotScanMapping(scans),
076      ScanMapper.class, ImmutableBytesWritable.class, ImmutableBytesWritable.class, job, true,
077      restoreDir);
078  }
079
080  protected Map<String, Collection<Scan>> getSnapshotScanMapping(final List<Scan> scans) {
081    return Multimaps.index(scans, new Function<Scan, String>() {
082      @Nullable
083      @Override
084      public String apply(Scan input) {
085        return snapshotNameForTable(
086          Bytes.toStringBinary(input.getAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME)));
087      }
088    }).asMap();
089  }
090
091  public static String snapshotNameForTable(String tableName) {
092    return tableName + "_snapshot";
093  }
094
095}