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.
074   * @throws InterruptedException When the job is aborted.
075   * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentKey()
076   */
077  @Override
078  public ImmutableBytesWritable getCurrentKey() throws IOException, InterruptedException {
079    return this.recordReaderImpl.getCurrentKey();
080  }
081
082  /**
083   * Returns the current value.
084   * @return The current value.
085   * @throws IOException          When the value is faulty.
086   * @throws InterruptedException When the job is aborted.
087   * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentValue()
088   */
089  @Override
090  public Result getCurrentValue() throws IOException, InterruptedException {
091    return this.recordReaderImpl.getCurrentValue();
092  }
093
094  /**
095   * Initializes the reader.
096   * @param inputsplit The split to work with.
097   * @param context    The current task context.
098   * @throws IOException          When setting up the reader fails.
099   * @throws InterruptedException When the job is aborted.
100   * @see org.apache.hadoop.mapreduce.RecordReader#initialize(
101   *      org.apache.hadoop.mapreduce.InputSplit, org.apache.hadoop.mapreduce.TaskAttemptContext)
102   */
103  @Override
104  public void initialize(InputSplit inputsplit, TaskAttemptContext context)
105    throws IOException, InterruptedException {
106    this.recordReaderImpl.initialize(inputsplit, context);
107  }
108
109  /**
110   * Positions the record reader to the next record.
111   * @return <code>true</code> if there was another record.
112   * @throws IOException          When reading the record failed.
113   * @throws InterruptedException When the job was aborted.
114   * @see org.apache.hadoop.mapreduce.RecordReader#nextKeyValue()
115   */
116  @Override
117  public boolean nextKeyValue() throws IOException, InterruptedException {
118    return this.recordReaderImpl.nextKeyValue();
119  }
120
121  /**
122   * The current progress of the record reader through its data.
123   * @return A number between 0.0 and 1.0, the fraction of the data read.
124   * @see org.apache.hadoop.mapreduce.RecordReader#getProgress()
125   */
126  @Override
127  public float getProgress() {
128    return this.recordReaderImpl.getProgress();
129  }
130
131}