View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.mapreduce;
20  
21  import java.io.IOException;
22  
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.client.HTable;
26  import org.apache.hadoop.hbase.client.Result;
27  import org.apache.hadoop.hbase.client.Scan;
28  import org.apache.hadoop.hbase.client.Table;
29  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
30  import org.apache.hadoop.mapreduce.InputSplit;
31  import org.apache.hadoop.mapreduce.RecordReader;
32  import org.apache.hadoop.mapreduce.TaskAttemptContext;
33  
34  /**
35   * Iterate over an HBase table data, return (ImmutableBytesWritable, Result)
36   * pairs.
37   */
38  @InterfaceAudience.Public
39  @InterfaceStability.Stable
40  public class TableRecordReader
41  extends RecordReader<ImmutableBytesWritable, Result> {
42  
43    private TableRecordReaderImpl recordReaderImpl = new TableRecordReaderImpl();
44  
45    /**
46     * Restart from survivable exceptions by creating a new scanner.
47     *
48     * @param firstRow  The first row to start at.
49     * @throws IOException When restarting fails.
50     */
51    public void restart(byte[] firstRow) throws IOException {
52      this.recordReaderImpl.restart(firstRow);
53    }
54  
55  
56    /**
57     * Sets the HBase table.
58     *
59     * @param htable  The {@link HTable} to scan.
60     */
61    public void setHTable(Table htable) {
62      this.recordReaderImpl.setHTable(htable);
63    }
64  
65    /**
66     * Sets the scan defining the actual details like columns etc.
67     *
68     * @param scan  The scan to set.
69     */
70    public void setScan(Scan scan) {
71      this.recordReaderImpl.setScan(scan);
72    }
73  
74    /**
75     * Closes the split.
76     *
77     * @see org.apache.hadoop.mapreduce.RecordReader#close()
78     */
79    @Override
80    public void close() {
81      this.recordReaderImpl.close();
82    }
83  
84    /**
85     * Returns the current key.
86     *
87     * @return The current key.
88     * @throws IOException
89     * @throws InterruptedException When the job is aborted.
90     * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentKey()
91     */
92    @Override
93    public ImmutableBytesWritable getCurrentKey() throws IOException,
94        InterruptedException {
95      return this.recordReaderImpl.getCurrentKey();
96    }
97  
98    /**
99     * Returns the current value.
100    *
101    * @return The current value.
102    * @throws IOException When the value is faulty.
103    * @throws InterruptedException When the job is aborted.
104    * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentValue()
105    */
106   @Override
107   public Result getCurrentValue() throws IOException, InterruptedException {
108     return this.recordReaderImpl.getCurrentValue();
109   }
110 
111   /**
112    * Initializes the reader.
113    *
114    * @param inputsplit  The split to work with.
115    * @param context  The current task context.
116    * @throws IOException When setting up the reader fails.
117    * @throws InterruptedException When the job is aborted.
118    * @see org.apache.hadoop.mapreduce.RecordReader#initialize(
119    *   org.apache.hadoop.mapreduce.InputSplit,
120    *   org.apache.hadoop.mapreduce.TaskAttemptContext)
121    */
122   @Override
123   public void initialize(InputSplit inputsplit,
124       TaskAttemptContext context) throws IOException,
125       InterruptedException {
126     this.recordReaderImpl.initialize(inputsplit, context);
127   }
128 
129   /**
130    * Positions the record reader to the next record.
131    *
132    * @return <code>true</code> if there was another record.
133    * @throws IOException When reading the record failed.
134    * @throws InterruptedException When the job was aborted.
135    * @see org.apache.hadoop.mapreduce.RecordReader#nextKeyValue()
136    */
137   @Override
138   public boolean nextKeyValue() throws IOException, InterruptedException {
139     return this.recordReaderImpl.nextKeyValue();
140   }
141 
142   /**
143    * The current progress of the record reader through its data.
144    *
145    * @return A number between 0.0 and 1.0, the fraction of the data read.
146    * @see org.apache.hadoop.mapreduce.RecordReader#getProgress()
147    */
148   @Override
149   public float getProgress() {
150     return this.recordReaderImpl.getProgress();
151   }
152 }