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.FSUtils;
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
057    TEST_UTIL.enableDebug(MultiTableSnapshotInputFormat.class);
058    TEST_UTIL.enableDebug(MultiTableSnapshotInputFormatImpl.class);
059
060    // take a snapshot of every table we have.
061    for (String tableName : TABLES) {
062      SnapshotTestingUtils
063          .createSnapshotAndValidate(TEST_UTIL.getAdmin(), TableName.valueOf(tableName),
064              ImmutableList.of(INPUT_FAMILY), null,
065              snapshotNameForTable(tableName), FSUtils.getRootDir(TEST_UTIL.getConfiguration()),
066              TEST_UTIL.getTestFileSystem(), true);
067    }
068  }
069
070  @Before
071  public void setUp() throws Exception {
072    this.restoreDir = TEST_UTIL.getRandomDir();
073  }
074
075  @Override
076  protected void initJob(List<Scan> scans, Job job) throws IOException {
077    TableMapReduceUtil
078        .initMultiTableSnapshotMapperJob(getSnapshotScanMapping(scans), ScanMapper.class,
079            ImmutableBytesWritable.class, ImmutableBytesWritable.class, job, true, restoreDir);
080  }
081
082  protected Map<String, Collection<Scan>> getSnapshotScanMapping(final List<Scan> scans) {
083    return Multimaps.index(scans, new Function<Scan, String>() {
084      @Nullable
085      @Override
086      public String apply(Scan input) {
087        return snapshotNameForTable(
088            Bytes.toStringBinary(input.getAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME)));
089      }
090    }).asMap();
091  }
092
093  public static String snapshotNameForTable(String tableName) {
094    return tableName + "_snapshot";
095  }
096
097}