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