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.mapred;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.classification.InterfaceStability;
23  import org.apache.hadoop.fs.Path;
24  import org.apache.hadoop.hbase.HRegionInfo;
25  import org.apache.hadoop.hbase.HTableDescriptor;
26  import org.apache.hadoop.hbase.client.Result;
27  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
28  import org.apache.hadoop.hbase.mapreduce.TableSnapshotInputFormatImpl;
29  import org.apache.hadoop.mapred.InputFormat;
30  import org.apache.hadoop.mapred.InputSplit;
31  import org.apache.hadoop.mapred.JobConf;
32  import org.apache.hadoop.mapred.RecordReader;
33  import org.apache.hadoop.mapred.Reporter;
34  
35  import java.io.DataInput;
36  import java.io.DataOutput;
37  import java.io.IOException;
38  import java.util.List;
39  
40  /**
41   * TableSnapshotInputFormat allows a MapReduce job to run over a table snapshot. Further
42   * documentation available on {@link org.apache.hadoop.hbase.mapreduce.TableSnapshotInputFormat}.
43   *
44   * @see org.apache.hadoop.hbase.mapreduce.TableSnapshotInputFormat
45   */
46  @InterfaceAudience.Public
47  @InterfaceStability.Evolving
48  public class TableSnapshotInputFormat implements InputFormat<ImmutableBytesWritable, Result> {
49  
50    public static class TableSnapshotRegionSplit implements InputSplit {
51      private TableSnapshotInputFormatImpl.InputSplit delegate;
52  
53      // constructor for mapreduce framework / Writable
54      public TableSnapshotRegionSplit() {
55        this.delegate = new TableSnapshotInputFormatImpl.InputSplit();
56      }
57  
58      public TableSnapshotRegionSplit(TableSnapshotInputFormatImpl.InputSplit delegate) {
59        this.delegate = delegate;
60      }
61  
62      public TableSnapshotRegionSplit(HTableDescriptor htd, HRegionInfo regionInfo,
63          List<String> locations) {
64        this.delegate = new TableSnapshotInputFormatImpl.InputSplit(htd, regionInfo, locations);
65      }
66  
67      @Override
68      public long getLength() throws IOException {
69        return delegate.getLength();
70      }
71  
72      @Override
73      public String[] getLocations() throws IOException {
74        return delegate.getLocations();
75      }
76  
77      @Override
78      public void write(DataOutput out) throws IOException {
79        delegate.write(out);
80      }
81  
82      @Override
83      public void readFields(DataInput in) throws IOException {
84        delegate.readFields(in);
85      }
86    }
87  
88    static class TableSnapshotRecordReader
89      implements RecordReader<ImmutableBytesWritable, Result> {
90  
91      private TableSnapshotInputFormatImpl.RecordReader delegate;
92  
93      public TableSnapshotRecordReader(TableSnapshotRegionSplit split, JobConf job)
94          throws IOException {
95        delegate = new TableSnapshotInputFormatImpl.RecordReader();
96        delegate.initialize(split.delegate, job);
97      }
98  
99      @Override
100     public boolean next(ImmutableBytesWritable key, Result value) throws IOException {
101       if (!delegate.nextKeyValue()) {
102         return false;
103       }
104       ImmutableBytesWritable currentKey = delegate.getCurrentKey();
105       key.set(currentKey.get(), currentKey.getOffset(), currentKey.getLength());
106       value.copyFrom(delegate.getCurrentValue());
107       return true;
108     }
109 
110     @Override
111     public ImmutableBytesWritable createKey() {
112       return new ImmutableBytesWritable();
113     }
114 
115     @Override
116     public Result createValue() {
117       return new Result();
118     }
119 
120     @Override
121     public long getPos() throws IOException {
122       return delegate.getPos();
123     }
124 
125     @Override
126     public void close() throws IOException {
127       delegate.close();
128     }
129 
130     @Override
131     public float getProgress() throws IOException {
132       return delegate.getProgress();
133     }
134   }
135 
136   @Override
137   public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException {
138     List<TableSnapshotInputFormatImpl.InputSplit> splits =
139       TableSnapshotInputFormatImpl.getSplits(job);
140     InputSplit[] results = new InputSplit[splits.size()];
141     for (int i = 0; i < splits.size(); i++) {
142       results[i] = new TableSnapshotRegionSplit(splits.get(i));
143     }
144     return results;
145   }
146 
147   @Override
148   public RecordReader<ImmutableBytesWritable, Result>
149   getRecordReader(InputSplit split, JobConf job, Reporter reporter) throws IOException {
150     return new TableSnapshotRecordReader((TableSnapshotRegionSplit) split, job);
151   }
152 
153   /**
154    * Configures the job to use TableSnapshotInputFormat to read from a snapshot.
155    * @param job the job to configure
156    * @param snapshotName the name of the snapshot to read from
157    * @param restoreDir a temporary directory to restore the snapshot into. Current user should
158    * have write permissions to this directory, and this should not be a subdirectory of rootdir.
159    * After the job is finished, restoreDir can be deleted.
160    * @throws IOException if an error occurs
161    */
162   public static void setInput(JobConf job, String snapshotName, Path restoreDir)
163       throws IOException {
164     TableSnapshotInputFormatImpl.setInput(job, snapshotName, restoreDir);
165   }
166 }