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