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    /**
56     * Sets the HBase table.
57     *
58     * @param htable  The table to scan.
59     * @deprecated Use setTable() instead.
60     */
61    @Deprecated
62    public void setHTable(Table htable) {
63      this.setTable(htable);
64    }
65  
66    /**
67     * @param table the {@link Table} to scan.
68     */
69    public void setTable(Table table) {
70      this.recordReaderImpl.setHTable(table);
71    }
72  
73    /**
74     * Sets the scan defining the actual details like columns etc.
75     *
76     * @param scan  The scan to set.
77     */
78    public void setScan(Scan scan) {
79      this.recordReaderImpl.setScan(scan);
80    }
81  
82    /**
83     * Closes the split.
84     *
85     * @see org.apache.hadoop.mapreduce.RecordReader#close()
86     */
87    @Override
88    public void close() {
89      this.recordReaderImpl.close();
90    }
91  
92    /**
93     * Returns the current key.
94     *
95     * @return The current key.
96     * @throws IOException
97     * @throws InterruptedException When the job is aborted.
98     * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentKey()
99     */
100   @Override
101   public ImmutableBytesWritable getCurrentKey() throws IOException,
102       InterruptedException {
103     return this.recordReaderImpl.getCurrentKey();
104   }
105 
106   /**
107    * Returns the current value.
108    *
109    * @return The current value.
110    * @throws IOException When the value is faulty.
111    * @throws InterruptedException When the job is aborted.
112    * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentValue()
113    */
114   @Override
115   public Result getCurrentValue() throws IOException, InterruptedException {
116     return this.recordReaderImpl.getCurrentValue();
117   }
118 
119   /**
120    * Initializes the reader.
121    *
122    * @param inputsplit  The split to work with.
123    * @param context  The current task context.
124    * @throws IOException When setting up the reader fails.
125    * @throws InterruptedException When the job is aborted.
126    * @see org.apache.hadoop.mapreduce.RecordReader#initialize(
127    *   org.apache.hadoop.mapreduce.InputSplit,
128    *   org.apache.hadoop.mapreduce.TaskAttemptContext)
129    */
130   @Override
131   public void initialize(InputSplit inputsplit,
132       TaskAttemptContext context) throws IOException,
133       InterruptedException {
134     this.recordReaderImpl.initialize(inputsplit, context);
135   }
136 
137   /**
138    * Positions the record reader to the next record.
139    *
140    * @return <code>true</code> if there was another record.
141    * @throws IOException When reading the record failed.
142    * @throws InterruptedException When the job was aborted.
143    * @see org.apache.hadoop.mapreduce.RecordReader#nextKeyValue()
144    */
145   @Override
146   public boolean nextKeyValue() throws IOException, InterruptedException {
147     return this.recordReaderImpl.nextKeyValue();
148   }
149 
150   /**
151    * The current progress of the record reader through its data.
152    *
153    * @return A number between 0.0 and 1.0, the fraction of the data read.
154    * @see org.apache.hadoop.mapreduce.RecordReader#getProgress()
155    */
156   @Override
157   public float getProgress() {
158     return this.recordReaderImpl.getProgress();
159   }
160 }