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.mapred;
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.Table;
27  import org.apache.hadoop.hbase.filter.Filter;
28  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
29  import org.apache.hadoop.mapred.RecordReader;
30  
31  
32  /**
33   * Iterate over an HBase table data, return (Text, RowResult) pairs
34   */
35  @InterfaceAudience.Public
36  @InterfaceStability.Stable
37  public class TableRecordReader
38  implements RecordReader<ImmutableBytesWritable, Result> {
39  
40    private TableRecordReaderImpl recordReaderImpl = new TableRecordReaderImpl();
41  
42    /**
43     * Restart from survivable exceptions by creating a new scanner.
44     *
45     * @param firstRow
46     * @throws IOException
47     */
48    public void restart(byte[] firstRow) throws IOException {
49      this.recordReaderImpl.restart(firstRow);
50    }
51  
52    /**
53     * Build the scanner. Not done in constructor to allow for extension.
54     *
55     * @throws IOException
56     */
57    public void init() throws IOException {
58      this.recordReaderImpl.restart(this.recordReaderImpl.getStartRow());
59    }
60  
61    /**
62     * @param htable the {@link org.apache.hadoop.hbase.HTableDescriptor} to scan.
63     */
64    public void setHTable(Table htable) {
65      this.recordReaderImpl.setHTable(htable);
66    }
67  
68    /**
69     * @param inputColumns the columns to be placed in {@link Result}.
70     */
71    public void setInputColumns(final byte [][] inputColumns) {
72      this.recordReaderImpl.setInputColumns(inputColumns);
73    }
74  
75    /**
76     * @param startRow the first row in the split
77     */
78    public void setStartRow(final byte [] startRow) {
79      this.recordReaderImpl.setStartRow(startRow);
80    }
81  
82    /**
83     *
84     * @param endRow the last row in the split
85     */
86    public void setEndRow(final byte [] endRow) {
87      this.recordReaderImpl.setEndRow(endRow);
88    }
89  
90    /**
91     * @param rowFilter the {@link Filter} to be used.
92     */
93    public void setRowFilter(Filter rowFilter) {
94      this.recordReaderImpl.setRowFilter(rowFilter);
95    }
96  
97    public void close() {
98      this.recordReaderImpl.close();
99    }
100 
101   /**
102    * @return ImmutableBytesWritable
103    *
104    * @see org.apache.hadoop.mapred.RecordReader#createKey()
105    */
106   public ImmutableBytesWritable createKey() {
107     return this.recordReaderImpl.createKey();
108   }
109 
110   /**
111    * @return RowResult
112    *
113    * @see org.apache.hadoop.mapred.RecordReader#createValue()
114    */
115   public Result createValue() {
116     return this.recordReaderImpl.createValue();
117   }
118 
119   public long getPos() {
120 
121     // This should be the ordinal tuple in the range;
122     // not clear how to calculate...
123     return this.recordReaderImpl.getPos();
124   }
125 
126   public float getProgress() {
127     // Depends on the total number of tuples and getPos
128     return this.recordReaderImpl.getPos();
129   }
130 
131   /**
132    * @param key HStoreKey as input key.
133    * @param value MapWritable as input value
134    * @return true if there was more data
135    * @throws IOException
136    */
137   public boolean next(ImmutableBytesWritable key, Result value)
138   throws IOException {
139     return this.recordReaderImpl.next(key, value);
140   }
141 }