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.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.client.Result;
26  import org.apache.hadoop.hbase.client.Scan;
27  import org.apache.hadoop.hbase.client.Table;
28  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
29  import org.apache.hadoop.mapreduce.InputSplit;
30  import org.apache.hadoop.mapreduce.RecordReader;
31  import org.apache.hadoop.mapreduce.TaskAttemptContext;
32  
33  /**
34   * Iterate over an HBase table data, return (ImmutableBytesWritable, Result)
35   * pairs.
36   */
37  @InterfaceAudience.Public
38  @InterfaceStability.Stable
39  public class TableRecordReader
40  extends RecordReader<ImmutableBytesWritable, Result> {
41  
42    private TableRecordReaderImpl recordReaderImpl = new TableRecordReaderImpl();
43  
44    /**
45     * Restart from survivable exceptions by creating a new scanner.
46     *
47     * @param firstRow  The first row to start at.
48     * @throws IOException When restarting fails.
49     */
50    public void restart(byte[] firstRow) throws IOException {
51      this.recordReaderImpl.restart(firstRow);
52    }
53  
54    /**
55     * @param table the {@link Table} to scan.
56     */
57    public void setTable(Table table) {
58      this.recordReaderImpl.setHTable(table);
59    }
60  
61    /**
62     * Sets the scan defining the actual details like columns etc.
63     *
64     * @param scan  The scan to set.
65     */
66    public void setScan(Scan scan) {
67      this.recordReaderImpl.setScan(scan);
68    }
69  
70    /**
71     * Closes the split.
72     *
73     * @see org.apache.hadoop.mapreduce.RecordReader#close()
74     */
75    @Override
76    public void close() {
77      this.recordReaderImpl.close();
78    }
79  
80    /**
81     * Returns the current key.
82     *
83     * @return The current key.
84     * @throws IOException
85     * @throws InterruptedException When the job is aborted.
86     * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentKey()
87     */
88    @Override
89    public ImmutableBytesWritable getCurrentKey() throws IOException,
90        InterruptedException {
91      return this.recordReaderImpl.getCurrentKey();
92    }
93  
94    /**
95     * Returns the current value.
96     *
97     * @return The current value.
98     * @throws IOException When the value is faulty.
99     * @throws InterruptedException When the job is aborted.
100    * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentValue()
101    */
102   @Override
103   public Result getCurrentValue() throws IOException, InterruptedException {
104     return this.recordReaderImpl.getCurrentValue();
105   }
106 
107   /**
108    * Initializes the reader.
109    *
110    * @param inputsplit  The split to work with.
111    * @param context  The current task context.
112    * @throws IOException When setting up the reader fails.
113    * @throws InterruptedException When the job is aborted.
114    * @see org.apache.hadoop.mapreduce.RecordReader#initialize(
115    *   org.apache.hadoop.mapreduce.InputSplit,
116    *   org.apache.hadoop.mapreduce.TaskAttemptContext)
117    */
118   @Override
119   public void initialize(InputSplit inputsplit,
120       TaskAttemptContext context) throws IOException,
121       InterruptedException {
122     this.recordReaderImpl.initialize(inputsplit, context);
123   }
124 
125   /**
126    * Positions the record reader to the next record.
127    *
128    * @return <code>true</code> if there was another record.
129    * @throws IOException When reading the record failed.
130    * @throws InterruptedException When the job was aborted.
131    * @see org.apache.hadoop.mapreduce.RecordReader#nextKeyValue()
132    */
133   @Override
134   public boolean nextKeyValue() throws IOException, InterruptedException {
135     return this.recordReaderImpl.nextKeyValue();
136   }
137 
138   /**
139    * The current progress of the record reader through its data.
140    *
141    * @return A number between 0.0 and 1.0, the fraction of the data read.
142    * @see org.apache.hadoop.mapreduce.RecordReader#getProgress()
143    */
144   @Override
145   public float getProgress() {
146     return this.recordReaderImpl.getProgress();
147   }
148 
149 }