1 /** 2 * Copyright 2011 The Apache Software Foundation 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package org.apache.hadoop.hbase.mapreduce; 21 22 import java.io.IOException; 23 24 import org.apache.hadoop.hbase.client.HTable; 25 import org.apache.hadoop.hbase.client.Result; 26 import org.apache.hadoop.hbase.client.Scan; 27 import org.apache.hadoop.hbase.io.ImmutableBytesWritable; 28 import org.apache.hadoop.mapreduce.InputSplit; 29 import org.apache.hadoop.mapreduce.RecordReader; 30 import org.apache.hadoop.mapreduce.TaskAttemptContext; 31 32 /** 33 * Iterate over an HBase table data, return (ImmutableBytesWritable, Result) 34 * pairs. 35 */ 36 public class TableRecordReader 37 extends RecordReader<ImmutableBytesWritable, Result> { 38 39 private TableRecordReaderImpl recordReaderImpl = new TableRecordReaderImpl(); 40 41 /** 42 * Restart from survivable exceptions by creating a new scanner. 43 * 44 * @param firstRow The first row to start at. 45 * @throws IOException When restarting fails. 46 */ 47 public void restart(byte[] firstRow) throws IOException { 48 this.recordReaderImpl.restart(firstRow); 49 } 50 51 52 /** 53 * Sets the HBase table. 54 * 55 * @param htable The {@link HTable} to scan. 56 */ 57 public void setHTable(HTable htable) { 58 this.recordReaderImpl.setHTable(htable); 59 } 60 61 /** 62 * Sets the scan defining the actual details like columns etc. 63 * 64 * @param scan The scan to set. 65 */ 66 public void setScan(Scan scan) { 67 this.recordReaderImpl.setScan(scan); 68 } 69 70 /** 71 * Closes the split. 72 * 73 * @see org.apache.hadoop.mapreduce.RecordReader#close() 74 */ 75 @Override 76 public void close() { 77 this.recordReaderImpl.close(); 78 } 79 80 /** 81 * Returns the current key. 82 * 83 * @return The current key. 84 * @throws IOException 85 * @throws InterruptedException When the job is aborted. 86 * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentKey() 87 */ 88 @Override 89 public ImmutableBytesWritable getCurrentKey() throws IOException, 90 InterruptedException { 91 return this.recordReaderImpl.getCurrentKey(); 92 } 93 94 /** 95 * Returns the current value. 96 * 97 * @return The current value. 98 * @throws IOException When the value is faulty. 99 * @throws InterruptedException When the job is aborted. 100 * @see org.apache.hadoop.mapreduce.RecordReader#getCurrentValue() 101 */ 102 @Override 103 public Result getCurrentValue() throws IOException, InterruptedException { 104 return this.recordReaderImpl.getCurrentValue(); 105 } 106 107 /** 108 * Initializes the reader. 109 * 110 * @param inputsplit The split to work with. 111 * @param context The current task context. 112 * @throws IOException When setting up the reader fails. 113 * @throws InterruptedException When the job is aborted. 114 * @see org.apache.hadoop.mapreduce.RecordReader#initialize( 115 * org.apache.hadoop.mapreduce.InputSplit, 116 * org.apache.hadoop.mapreduce.TaskAttemptContext) 117 */ 118 @Override 119 public void initialize(InputSplit inputsplit, 120 TaskAttemptContext context) throws IOException, 121 InterruptedException { 122 this.recordReaderImpl.initialize(inputsplit, context); 123 } 124 125 /** 126 * Positions the record reader to the next record. 127 * 128 * @return <code>true</code> if there was another record. 129 * @throws IOException When reading the record failed. 130 * @throws InterruptedException When the job was aborted. 131 * @see org.apache.hadoop.mapreduce.RecordReader#nextKeyValue() 132 */ 133 @Override 134 public boolean nextKeyValue() throws IOException, InterruptedException { 135 return this.recordReaderImpl.nextKeyValue(); 136 } 137 138 /** 139 * The current progress of the record reader through its data. 140 * 141 * @return A number between 0.0 and 1.0, the fraction of the data read. 142 * @see org.apache.hadoop.mapreduce.RecordReader#getProgress() 143 */ 144 @Override 145 public float getProgress() { 146 return this.recordReaderImpl.getProgress(); 147 } 148 }