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. 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. 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 @Override 086 public void close() { 087 this.recordReaderImpl.close(); 088 } 089 090 /** 091 * @see org.apache.hadoop.mapred.RecordReader#createKey() 092 */ 093 @Override 094 public ImmutableBytesWritable createKey() { 095 return this.recordReaderImpl.createKey(); 096 } 097 098 /** 099 * @see org.apache.hadoop.mapred.RecordReader#createValue() 100 */ 101 @Override 102 public Result createValue() { 103 return this.recordReaderImpl.createValue(); 104 } 105 106 @Override 107 public long getPos() { 108 109 // This should be the ordinal tuple in the range; 110 // not clear how to calculate... 111 return this.recordReaderImpl.getPos(); 112 } 113 114 @Override 115 public float getProgress() { 116 // Depends on the total number of tuples and getPos 117 return this.recordReaderImpl.getPos(); 118 } 119 120 /** 121 * @param key HStoreKey as input key. 122 * @param value MapWritable as input value 123 * @return true if there was more data 124 */ 125 @Override 126 public boolean next(ImmutableBytesWritable key, Result value) throws IOException { 127 return this.recordReaderImpl.next(key, value); 128 } 129}