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 */ 018 019package org.apache.hadoop.hbase.mapreduce; 020 021import java.io.DataInput; 022import java.io.DataOutput; 023import java.io.IOException; 024import java.lang.reflect.Method; 025import java.util.ArrayList; 026import java.util.List; 027 028import org.apache.hadoop.fs.Path; 029import org.apache.hadoop.hbase.HRegionInfo; 030import org.apache.hadoop.hbase.HTableDescriptor; 031import org.apache.hadoop.hbase.client.RegionInfo; 032import org.apache.hadoop.hbase.client.Result; 033import org.apache.hadoop.hbase.client.Scan; 034import org.apache.hadoop.hbase.client.metrics.ScanMetrics; 035import org.apache.hadoop.hbase.io.ImmutableBytesWritable; 036import org.apache.hadoop.hbase.util.RegionSplitter; 037import org.apache.hadoop.io.Writable; 038import org.apache.hadoop.mapreduce.InputFormat; 039import org.apache.hadoop.mapreduce.InputSplit; 040import org.apache.hadoop.mapreduce.Job; 041import org.apache.hadoop.mapreduce.JobContext; 042import org.apache.hadoop.mapreduce.RecordReader; 043import org.apache.hadoop.mapreduce.TaskAttemptContext; 044import org.apache.yetus.audience.InterfaceAudience; 045 046import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; 047 048/** 049 * TableSnapshotInputFormat allows a MapReduce job to run over a table snapshot. The job 050 * bypasses HBase servers, and directly accesses the underlying files (hfile, recovered edits, 051 * wals, etc) directly to provide maximum performance. The snapshot is not required to be 052 * restored to the live cluster or cloned. This also allows to run the mapreduce job from an 053 * online or offline hbase cluster. The snapshot files can be exported by using the 054 * {@link org.apache.hadoop.hbase.snapshot.ExportSnapshot} tool, to a pure-hdfs cluster, 055 * and this InputFormat can be used to run the mapreduce job directly over the snapshot files. 056 * The snapshot should not be deleted while there are jobs reading from snapshot files. 057 * <p> 058 * Usage is similar to TableInputFormat, and 059 * {@link TableMapReduceUtil#initTableSnapshotMapperJob(String, Scan, Class, Class, Class, Job, boolean, Path)} 060 * can be used to configure the job. 061 * <pre>{@code 062 * Job job = new Job(conf); 063 * Scan scan = new Scan(); 064 * TableMapReduceUtil.initTableSnapshotMapperJob(snapshotName, 065 * scan, MyTableMapper.class, MyMapKeyOutput.class, 066 * MyMapOutputValueWritable.class, job, true); 067 * } 068 * </pre> 069 * <p> 070 * Internally, this input format restores the snapshot into the given tmp directory. By default, 071 * and similar to {@link TableInputFormat} an InputSplit is created per region, but optionally you 072 * can run N mapper tasks per every region, in which case the region key range will be split to 073 * N sub-ranges and an InputSplit will be created per sub-range. The region is opened for reading 074 * from each RecordReader. An internal RegionScanner is used to execute the 075 * {@link org.apache.hadoop.hbase.CellScanner} obtained from the user. 076 * <p> 077 * HBase owns all the data and snapshot files on the filesystem. Only the 'hbase' user can read from 078 * snapshot files and data files. 079 * To read from snapshot files directly from the file system, the user who is running the MR job 080 * must have sufficient permissions to access snapshot and reference files. 081 * This means that to run mapreduce over snapshot files, the MR job has to be run as the HBase 082 * user or the user must have group or other privileges in the filesystem (See HBASE-8369). 083 * Note that, given other users access to read from snapshot/data files will completely circumvent 084 * the access control enforced by HBase. 085 * @see org.apache.hadoop.hbase.client.TableSnapshotScanner 086 */ 087@InterfaceAudience.Public 088public class TableSnapshotInputFormat extends InputFormat<ImmutableBytesWritable, Result> { 089 090 public static class TableSnapshotRegionSplit extends InputSplit implements Writable { 091 private TableSnapshotInputFormatImpl.InputSplit delegate; 092 093 // constructor for mapreduce framework / Writable 094 public TableSnapshotRegionSplit() { 095 this.delegate = new TableSnapshotInputFormatImpl.InputSplit(); 096 } 097 098 public TableSnapshotRegionSplit(TableSnapshotInputFormatImpl.InputSplit delegate) { 099 this.delegate = delegate; 100 } 101 102 public TableSnapshotRegionSplit(HTableDescriptor htd, HRegionInfo regionInfo, 103 List<String> locations, Scan scan, Path restoreDir) { 104 this.delegate = 105 new TableSnapshotInputFormatImpl.InputSplit(htd, regionInfo, locations, scan, restoreDir); 106 } 107 108 @Override 109 public long getLength() throws IOException, InterruptedException { 110 return delegate.getLength(); 111 } 112 113 @Override 114 public String[] getLocations() throws IOException, InterruptedException { 115 return delegate.getLocations(); 116 } 117 118 @Override 119 public void write(DataOutput out) throws IOException { 120 delegate.write(out); 121 } 122 123 @Override 124 public void readFields(DataInput in) throws IOException { 125 delegate.readFields(in); 126 } 127 128 /** 129 * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0 130 * Use {@link #getRegion()} 131 */ 132 @Deprecated 133 public HRegionInfo getRegionInfo() { 134 return delegate.getRegionInfo(); 135 } 136 137 public RegionInfo getRegion() { 138 return delegate.getRegionInfo(); 139 } 140 } 141 142 @VisibleForTesting 143 static class TableSnapshotRegionRecordReader extends 144 RecordReader<ImmutableBytesWritable, Result> { 145 private TableSnapshotInputFormatImpl.RecordReader delegate = 146 new TableSnapshotInputFormatImpl.RecordReader(); 147 private TaskAttemptContext context; 148 private Method getCounter; 149 150 @Override 151 public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, 152 InterruptedException { 153 this.context = context; 154 getCounter = TableRecordReaderImpl.retrieveGetCounterWithStringsParams(context); 155 delegate.initialize( 156 ((TableSnapshotRegionSplit) split).delegate, 157 context.getConfiguration()); 158 } 159 160 @Override 161 public boolean nextKeyValue() throws IOException, InterruptedException { 162 boolean result = delegate.nextKeyValue(); 163 if (result) { 164 ScanMetrics scanMetrics = delegate.getScanner().getScanMetrics(); 165 if (scanMetrics != null && context != null) { 166 TableRecordReaderImpl.updateCounters(scanMetrics, 0, getCounter, context, 0); 167 } 168 } 169 return result; 170 } 171 172 @Override 173 public ImmutableBytesWritable getCurrentKey() throws IOException, InterruptedException { 174 return delegate.getCurrentKey(); 175 } 176 177 @Override 178 public Result getCurrentValue() throws IOException, InterruptedException { 179 return delegate.getCurrentValue(); 180 } 181 182 @Override 183 public float getProgress() throws IOException, InterruptedException { 184 return delegate.getProgress(); 185 } 186 187 @Override 188 public void close() throws IOException { 189 delegate.close(); 190 } 191 } 192 193 @Override 194 public RecordReader<ImmutableBytesWritable, Result> createRecordReader( 195 InputSplit split, TaskAttemptContext context) throws IOException { 196 return new TableSnapshotRegionRecordReader(); 197 } 198 199 @Override 200 public List<InputSplit> getSplits(JobContext job) throws IOException, InterruptedException { 201 List<InputSplit> results = new ArrayList<>(); 202 for (TableSnapshotInputFormatImpl.InputSplit split : 203 TableSnapshotInputFormatImpl.getSplits(job.getConfiguration())) { 204 results.add(new TableSnapshotRegionSplit(split)); 205 } 206 return results; 207 } 208 209 /** 210 * Configures the job to use TableSnapshotInputFormat to read from a snapshot. 211 * @param job the job to configure 212 * @param snapshotName the name of the snapshot to read from 213 * @param restoreDir a temporary directory to restore the snapshot into. Current user should 214 * have write permissions to this directory, and this should not be a subdirectory of rootdir. 215 * After the job is finished, restoreDir can be deleted. 216 * @throws IOException if an error occurs 217 */ 218 public static void setInput(Job job, String snapshotName, Path restoreDir) 219 throws IOException { 220 TableSnapshotInputFormatImpl.setInput(job.getConfiguration(), snapshotName, restoreDir); 221 } 222 223 /** 224 * Configures the job to use TableSnapshotInputFormat to read from a snapshot. 225 * @param job the job to configure 226 * @param snapshotName the name of the snapshot to read from 227 * @param restoreDir a temporary directory to restore the snapshot into. Current user should 228 * have write permissions to this directory, and this should not be a subdirectory of rootdir. 229 * After the job is finished, restoreDir can be deleted. 230 * @param splitAlgo split algorithm to generate splits from region 231 * @param numSplitsPerRegion how many input splits to generate per one region 232 * @throws IOException if an error occurs 233 */ 234 public static void setInput(Job job, String snapshotName, Path restoreDir, 235 RegionSplitter.SplitAlgorithm splitAlgo, int numSplitsPerRegion) throws IOException { 236 TableSnapshotInputFormatImpl.setInput(job.getConfiguration(), snapshotName, restoreDir, 237 splitAlgo, numSplitsPerRegion); 238 } 239}