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.annotations.VisibleForTesting;
22  import org.apache.hadoop.fs.Path;
23  import org.apache.hadoop.hbase.HRegionInfo;
24  import org.apache.hadoop.hbase.HTableDescriptor;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.client.Result;
28  import org.apache.hadoop.hbase.client.Scan;
29  import org.apache.hadoop.hbase.client.metrics.ScanMetrics;
30  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
31  import org.apache.hadoop.hbase.util.RegionSplitter;
32  import org.apache.hadoop.io.Writable;
33  import org.apache.hadoop.mapreduce.InputFormat;
34  import org.apache.hadoop.mapreduce.InputSplit;
35  import org.apache.hadoop.mapreduce.Job;
36  import org.apache.hadoop.mapreduce.JobContext;
37  import org.apache.hadoop.mapreduce.RecordReader;
38  import org.apache.hadoop.mapreduce.TaskAttemptContext;
39  
40  import java.io.DataInput;
41  import java.io.DataOutput;
42  import java.io.IOException;
43  import java.lang.reflect.Method;
44  import java.util.ArrayList;
45  import java.util.List;
46  
47  /**
48   * TableSnapshotInputFormat allows a MapReduce job to run over a table snapshot. The job
49   * bypasses HBase servers, and directly accesses the underlying files (hfile, recovered edits,
50   * wals, etc) directly to provide maximum performance. The snapshot is not required to be
51   * restored to the live cluster or cloned. This also allows to run the mapreduce job from an
52   * online or offline hbase cluster. The snapshot files can be exported by using the
53   * {@link org.apache.hadoop.hbase.snapshot.ExportSnapshot} tool, to a pure-hdfs cluster, 
54   * and this InputFormat can be used to run the mapreduce job directly over the snapshot files. 
55   * The snapshot should not be deleted while there are jobs reading from snapshot files.
56   * <p>
57   * Usage is similar to TableInputFormat, and
58   * {@link TableMapReduceUtil#initTableSnapshotMapperJob(String, Scan, Class, Class, Class, Job,
59   *   boolean, Path)}
60   * can be used to configure the job.
61   * <pre>{@code
62   * Job job = new Job(conf);
63   * Scan scan = new Scan();
64   * TableMapReduceUtil.initTableSnapshotMapperJob(snapshotName,
65   *      scan, MyTableMapper.class, MyMapKeyOutput.class,
66   *      MyMapOutputValueWritable.class, job, true);
67   * }
68   * </pre>
69   * <p>
70   * Internally, this input format restores the snapshot into the given tmp directory. By default,
71   * and similar to {@link TableInputFormat} an InputSplit is created per region, but optionally you
72   * can run N mapper tasks per every region, in which case the region key range will be split to
73   * N sub-ranges and an InputSplit will be created per sub-range. The region is opened for reading
74   * from each RecordReader. An internal RegionScanner is used to execute the 
75   * {@link org.apache.hadoop.hbase.CellScanner} obtained from the user.
76   * <p>
77   * HBase owns all the data and snapshot files on the filesystem. Only the 'hbase' user can read from
78   * snapshot files and data files.
79   * To read from snapshot files directly from the file system, the user who is running the MR job
80   * must have sufficient permissions to access snapshot and reference files.
81   * This means that to run mapreduce over snapshot files, the MR job has to be run as the HBase
82   * user or the user must have group or other privileges in the filesystem (See HBASE-8369).
83   * Note that, given other users access to read from snapshot/data files will completely circumvent
84   * the access control enforced by HBase.
85   * @see org.apache.hadoop.hbase.client.TableSnapshotScanner
86   */
87  @InterfaceAudience.Public
88  @InterfaceStability.Evolving
89  public class TableSnapshotInputFormat extends InputFormat<ImmutableBytesWritable, Result> {
90  
91    public static class TableSnapshotRegionSplit extends InputSplit implements Writable {
92      private TableSnapshotInputFormatImpl.InputSplit delegate;
93  
94      // constructor for mapreduce framework / Writable
95      public TableSnapshotRegionSplit() {
96        this.delegate = new TableSnapshotInputFormatImpl.InputSplit();
97      }
98  
99      public TableSnapshotRegionSplit(TableSnapshotInputFormatImpl.InputSplit delegate) {
100       this.delegate = delegate;
101     }
102 
103     public TableSnapshotRegionSplit(HTableDescriptor htd, HRegionInfo regionInfo,
104         List<String> locations, Scan scan, Path restoreDir) {
105       this.delegate =
106           new TableSnapshotInputFormatImpl.InputSplit(htd, regionInfo, locations, scan, restoreDir);
107     }
108 
109     @Override
110     public long getLength() throws IOException, InterruptedException {
111       return delegate.getLength();
112     }
113 
114     @Override
115     public String[] getLocations() throws IOException, InterruptedException {
116       return delegate.getLocations();
117     }
118 
119     @Override
120     public void write(DataOutput out) throws IOException {
121       delegate.write(out);
122     }
123 
124     @Override
125     public void readFields(DataInput in) throws IOException {
126       delegate.readFields(in);
127     }
128 
129     TableSnapshotInputFormatImpl.InputSplit getDelegate() {
130       return this.delegate;
131     }
132   }
133 
134   @VisibleForTesting
135   static class TableSnapshotRegionRecordReader extends
136       RecordReader<ImmutableBytesWritable, Result> {
137     private TableSnapshotInputFormatImpl.RecordReader delegate =
138       new TableSnapshotInputFormatImpl.RecordReader();
139     private TaskAttemptContext context;
140     private Method getCounter;
141 
142     @Override
143     public void initialize(InputSplit split, TaskAttemptContext context) throws IOException,
144         InterruptedException {
145       this.context = context;
146       getCounter = TableRecordReaderImpl.retrieveGetCounterWithStringsParams(context);
147       delegate.initialize(
148         ((TableSnapshotRegionSplit) split).delegate,
149         context.getConfiguration());
150     }
151 
152     @Override
153     public boolean nextKeyValue() throws IOException, InterruptedException {
154       boolean result = delegate.nextKeyValue();
155       if (result) {
156         ScanMetrics scanMetrics = delegate.getScanner().getScanMetrics();
157         if (scanMetrics != null && context != null) {
158           TableRecordReaderImpl.updateCounters(scanMetrics, 0, getCounter, context, 0);
159         }
160       }
161       return result;
162     }
163 
164     @Override
165     public ImmutableBytesWritable getCurrentKey() throws IOException, InterruptedException {
166       return delegate.getCurrentKey();
167     }
168 
169     @Override
170     public Result getCurrentValue() throws IOException, InterruptedException {
171       return delegate.getCurrentValue();
172     }
173 
174     @Override
175     public float getProgress() throws IOException, InterruptedException {
176       return delegate.getProgress();
177     }
178 
179     @Override
180     public void close() throws IOException {
181       delegate.close();
182     }
183   }
184 
185   @Override
186   public RecordReader<ImmutableBytesWritable, Result> createRecordReader(
187       InputSplit split, TaskAttemptContext context) throws IOException {
188     return new TableSnapshotRegionRecordReader();
189   }
190 
191   @Override
192   public List<InputSplit> getSplits(JobContext job) throws IOException, InterruptedException {
193     List<InputSplit> results = new ArrayList<InputSplit>();
194     for (TableSnapshotInputFormatImpl.InputSplit split :
195         TableSnapshotInputFormatImpl.getSplits(job.getConfiguration())) {
196       results.add(new TableSnapshotRegionSplit(split));
197     }
198     return results;
199   }
200 
201   /**
202    * Configures the job to use TableSnapshotInputFormat to read from a snapshot.
203    * @param job the job to configure
204    * @param snapshotName the name of the snapshot to read from
205    * @param restoreDir a temporary directory to restore the snapshot into. Current user should
206    * have write permissions to this directory, and this should not be a subdirectory of rootdir.
207    * After the job is finished, restoreDir can be deleted.
208    * @throws IOException if an error occurs
209    */
210   public static void setInput(Job job, String snapshotName, Path restoreDir)
211       throws IOException {
212     TableSnapshotInputFormatImpl.setInput(job.getConfiguration(), snapshotName, restoreDir);
213   }
214 
215   /**
216    * Configures the job to use TableSnapshotInputFormat to read from a snapshot.
217    * @param job the job to configure
218    * @param snapshotName the name of the snapshot to read from
219    * @param restoreDir a temporary directory to restore the snapshot into. Current user should
220    * have write permissions to this directory, and this should not be a subdirectory of rootdir.
221    * After the job is finished, restoreDir can be deleted.
222    * @param splitAlgo split algorithm to generate splits from region
223    * @param numSplitsPerRegion how many input splits to generate per one region
224    * @throws IOException if an error occurs
225    */
226   public static void setInput(Job job, String snapshotName, Path restoreDir,
227                               RegionSplitter.SplitAlgorithm splitAlgo, int numSplitsPerRegion) throws IOException {
228     TableSnapshotInputFormatImpl.setInput(job.getConfiguration(), snapshotName, restoreDir,
229             splitAlgo, numSplitsPerRegion);
230   }
231 }