001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.mapred;
020
021import java.io.IOException;
022
023import org.apache.yetus.audience.InterfaceAudience;
024import org.apache.hadoop.hbase.client.Result;
025import org.apache.hadoop.hbase.client.Table;
026import org.apache.hadoop.hbase.filter.Filter;
027import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
028import org.apache.hadoop.mapred.RecordReader;
029
030
031/**
032 * Iterate over an HBase table data, return (Text, RowResult) pairs
033 */
034@InterfaceAudience.Public
035public class TableRecordReader
036implements RecordReader<ImmutableBytesWritable, Result> {
037
038  private TableRecordReaderImpl recordReaderImpl = new TableRecordReaderImpl();
039
040  /**
041   * Restart from survivable exceptions by creating a new scanner.
042   *
043   * @param firstRow
044   * @throws IOException
045   */
046  public void restart(byte[] firstRow) throws IOException {
047    this.recordReaderImpl.restart(firstRow);
048  }
049
050  /**
051   * Build the scanner. Not done in constructor to allow for extension.
052   *
053   * @throws IOException
054   */
055  public void init() throws IOException {
056    this.recordReaderImpl.restart(this.recordReaderImpl.getStartRow());
057  }
058
059  /**
060   * @param htable the {@link org.apache.hadoop.hbase.HTableDescriptor} to scan.
061   */
062  public void setHTable(Table htable) {
063    this.recordReaderImpl.setHTable(htable);
064  }
065
066  /**
067   * @param inputColumns the columns to be placed in {@link Result}.
068   */
069  public void setInputColumns(final byte [][] inputColumns) {
070    this.recordReaderImpl.setInputColumns(inputColumns);
071  }
072
073  /**
074   * @param startRow the first row in the split
075   */
076  public void setStartRow(final byte [] startRow) {
077    this.recordReaderImpl.setStartRow(startRow);
078  }
079
080  /**
081   *
082   * @param endRow the last row in the split
083   */
084  public void setEndRow(final byte [] endRow) {
085    this.recordReaderImpl.setEndRow(endRow);
086  }
087
088  /**
089   * @param rowFilter the {@link Filter} to be used.
090   */
091  public void setRowFilter(Filter rowFilter) {
092    this.recordReaderImpl.setRowFilter(rowFilter);
093  }
094
095  public void close() {
096    this.recordReaderImpl.close();
097  }
098
099  /**
100   * @return ImmutableBytesWritable
101   *
102   * @see org.apache.hadoop.mapred.RecordReader#createKey()
103   */
104  public ImmutableBytesWritable createKey() {
105    return this.recordReaderImpl.createKey();
106  }
107
108  /**
109   * @return RowResult
110   *
111   * @see org.apache.hadoop.mapred.RecordReader#createValue()
112   */
113  public Result createValue() {
114    return this.recordReaderImpl.createValue();
115  }
116
117  public long getPos() {
118
119    // This should be the ordinal tuple in the range;
120    // not clear how to calculate...
121    return this.recordReaderImpl.getPos();
122  }
123
124  public float getProgress() {
125    // Depends on the total number of tuples and getPos
126    return this.recordReaderImpl.getPos();
127  }
128
129  /**
130   * @param key HStoreKey as input key.
131   * @param value MapWritable as input value
132   * @return true if there was more data
133   * @throws IOException
134   */
135  public boolean next(ImmutableBytesWritable key, Result value)
136  throws IOException {
137    return this.recordReaderImpl.next(key, value);
138  }
139}