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.mapreduce;
019
020import java.io.IOException;
021import org.apache.hadoop.hbase.client.Result;
022import org.apache.hadoop.hbase.client.Scan;
023import org.apache.hadoop.hbase.client.Table;
024import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
025import org.apache.hadoop.mapreduce.InputSplit;
026import org.apache.hadoop.mapreduce.RecordReader;
027import org.apache.hadoop.mapreduce.TaskAttemptContext;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * Iterate over an HBase table data, return (ImmutableBytesWritable, Result) pairs.
032 */
033@InterfaceAudience.Public
034public class TableRecordReader extends RecordReader<ImmutableBytesWritable, Result> {
035
036  private TableRecordReaderImpl recordReaderImpl = new TableRecordReaderImpl();
037
038  /**
039   * Restart from survivable exceptions by creating a new scanner.
040   * @param firstRow The first row to start at.
041   * @throws IOException When restarting fails.
042   */
043  public void restart(byte[] firstRow) throws IOException {
044    this.recordReaderImpl.restart(firstRow);
045  }
046
047  /**
048   * @param table the {@link Table} to scan.
049   */
050  public void setTable(Table table) {
051    this.recordReaderImpl.setHTable(table);
052  }
053
054  /**
055   * Sets the scan defining the actual details like columns etc.
056   * @param scan The scan to set.
057   */
058  public void setScan(Scan scan) {
059    this.recordReaderImpl.setScan(scan);
060  }
061
062  /**
063   * Closes the split.
064   * @see org.apache.hadoop.mapreduce.RecordReader#close()
065   */
066  @Override
067  public void close() {
068    this.recordReaderImpl.close();
069  }
070
071  /**
072   * Returns the current key.
073   * @return The current key. n * @throws InterruptedException When the job is aborted.
074   * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentKey()
075   */
076  @Override
077  public ImmutableBytesWritable getCurrentKey() throws IOException, InterruptedException {
078    return this.recordReaderImpl.getCurrentKey();
079  }
080
081  /**
082   * Returns the current value.
083   * @return The current value.
084   * @throws IOException          When the value is faulty.
085   * @throws InterruptedException When the job is aborted.
086   * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentValue()
087   */
088  @Override
089  public Result getCurrentValue() throws IOException, InterruptedException {
090    return this.recordReaderImpl.getCurrentValue();
091  }
092
093  /**
094   * Initializes the reader.
095   * @param inputsplit The split to work with.
096   * @param context    The current task context.
097   * @throws IOException          When setting up the reader fails.
098   * @throws InterruptedException When the job is aborted.
099   * @see org.apache.hadoop.mapreduce.RecordReader#initialize(
100   *      org.apache.hadoop.mapreduce.InputSplit, org.apache.hadoop.mapreduce.TaskAttemptContext)
101   */
102  @Override
103  public void initialize(InputSplit inputsplit, TaskAttemptContext context)
104    throws IOException, InterruptedException {
105    this.recordReaderImpl.initialize(inputsplit, context);
106  }
107
108  /**
109   * Positions the record reader to the next record.
110   * @return <code>true</code> if there was another record.
111   * @throws IOException          When reading the record failed.
112   * @throws InterruptedException When the job was aborted.
113   * @see org.apache.hadoop.mapreduce.RecordReader#nextKeyValue()
114   */
115  @Override
116  public boolean nextKeyValue() throws IOException, InterruptedException {
117    return this.recordReaderImpl.nextKeyValue();
118  }
119
120  /**
121   * The current progress of the record reader through its data.
122   * @return A number between 0.0 and 1.0, the fraction of the data read.
123   * @see org.apache.hadoop.mapreduce.RecordReader#getProgress()
124   */
125  @Override
126  public float getProgress() {
127    return this.recordReaderImpl.getProgress();
128  }
129
130}