View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.mapred;
20  
21  import java.io.IOException;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.classification.InterfaceStability;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.client.HTable;
29  import org.apache.hadoop.hbase.client.Result;
30  import org.apache.hadoop.hbase.filter.Filter;
31  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
32  import org.apache.hadoop.hbase.regionserver.HRegion;
33  import org.apache.hadoop.mapred.InputFormat;
34  import org.apache.hadoop.mapred.InputSplit;
35  import org.apache.hadoop.mapred.JobConf;
36  import org.apache.hadoop.mapred.RecordReader;
37  import org.apache.hadoop.mapred.Reporter;
38  
39  /**
40   * A Base for {@link TableInputFormat}s. Receives a {@link HTable}, a
41   * byte[] of input columns and optionally a {@link Filter}.
42   * Subclasses may use other TableRecordReader implementations.
43   * <p>
44   * An example of a subclass:
45   * <pre>
46   *   class ExampleTIF extends TableInputFormatBase implements JobConfigurable {
47   *
48   *     public void configure(JobConf job) {
49   *       HTable exampleTable = new HTable(HBaseConfiguration.create(job),
50   *         Bytes.toBytes("exampleTable"));
51   *       // mandatory
52   *       setHTable(exampleTable);
53   *       Text[] inputColumns = new byte [][] { Bytes.toBytes("columnA"),
54   *         Bytes.toBytes("columnB") };
55   *       // mandatory
56   *       setInputColumns(inputColumns);
57   *       RowFilterInterface exampleFilter = new RegExpRowFilter("keyPrefix.*");
58   *       // optional
59   *       setRowFilter(exampleFilter);
60   *     }
61   *
62   *     public void validateInput(JobConf job) throws IOException {
63   *     }
64   *  }
65   * </pre>
66   */
67  
68  @InterfaceAudience.Public
69  @InterfaceStability.Stable
70  public abstract class TableInputFormatBase
71  implements InputFormat<ImmutableBytesWritable, Result> {
72    private static final Log LOG = LogFactory.getLog(TableInputFormatBase.class);
73    private byte [][] inputColumns;
74    private HTable table;
75    private TableRecordReader tableRecordReader;
76    private Filter rowFilter;
77  
78    /**
79     * Builds a TableRecordReader. If no TableRecordReader was provided, uses
80     * the default.
81     *
82     * @see org.apache.hadoop.mapred.InputFormat#getRecordReader(InputSplit,
83     *      JobConf, Reporter)
84     */
85    public RecordReader<ImmutableBytesWritable, Result> getRecordReader(
86        InputSplit split, JobConf job, Reporter reporter)
87    throws IOException {
88      TableSplit tSplit = (TableSplit) split;
89      TableRecordReader trr = this.tableRecordReader;
90      // if no table record reader was provided use default
91      if (trr == null) {
92        trr = new TableRecordReader();
93      }
94      trr.setStartRow(tSplit.getStartRow());
95      trr.setEndRow(tSplit.getEndRow());
96      trr.setHTable(this.table);
97      trr.setInputColumns(this.inputColumns);
98      trr.setRowFilter(this.rowFilter);
99      trr.init();
100     return trr;
101   }
102 
103   /**
104    * Calculates the splits that will serve as input for the map tasks.
105    * <ul>
106    * Splits are created in number equal to the smallest between numSplits and
107    * the number of {@link HRegion}s in the table. If the number of splits is
108    * smaller than the number of {@link HRegion}s then splits are spanned across
109    * multiple {@link HRegion}s and are grouped the most evenly possible. In the
110    * case splits are uneven the bigger splits are placed first in the
111    * {@link InputSplit} array.
112    *
113    * @param job the map task {@link JobConf}
114    * @param numSplits a hint to calculate the number of splits (mapred.map.tasks).
115    *
116    * @return the input splits
117    *
118    * @see org.apache.hadoop.mapred.InputFormat#getSplits(org.apache.hadoop.mapred.JobConf, int)
119    */
120   public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException {
121     if (this.table == null) {
122       throw new IOException("No table was provided");
123     }
124     byte [][] startKeys = this.table.getStartKeys();
125     if (startKeys == null || startKeys.length == 0) {
126       throw new IOException("Expecting at least one region");
127     }
128     if (this.inputColumns == null || this.inputColumns.length == 0) {
129       throw new IOException("Expecting at least one column");
130     }
131     int realNumSplits = numSplits > startKeys.length? startKeys.length:
132       numSplits;
133     InputSplit[] splits = new InputSplit[realNumSplits];
134     int middle = startKeys.length / realNumSplits;
135     int startPos = 0;
136     for (int i = 0; i < realNumSplits; i++) {
137       int lastPos = startPos + middle;
138       lastPos = startKeys.length % realNumSplits > i ? lastPos + 1 : lastPos;
139       String regionLocation = table.getRegionLocation(startKeys[startPos]).
140         getHostname();
141       splits[i] = new TableSplit(this.table.getName(),
142         startKeys[startPos], ((i + 1) < realNumSplits) ? startKeys[lastPos]:
143           HConstants.EMPTY_START_ROW, regionLocation);
144       LOG.info("split: " + i + "->" + splits[i]);
145       startPos = lastPos;
146     }
147     return splits;
148   }
149 
150   /**
151    * @param inputColumns to be passed in {@link Result} to the map task.
152    */
153   protected void setInputColumns(byte [][] inputColumns) {
154     this.inputColumns = inputColumns;
155   }
156 
157   /**
158    * Allows subclasses to get the {@link HTable}.
159    */
160   protected HTable getHTable() {
161     return this.table;
162   }
163 
164   /**
165    * Allows subclasses to set the {@link HTable}.
166    *
167    * @param table to get the data from
168    */
169   protected void setHTable(HTable table) {
170     this.table = table;
171   }
172 
173   /**
174    * Allows subclasses to set the {@link TableRecordReader}.
175    *
176    * @param tableRecordReader
177    *                to provide other {@link TableRecordReader} implementations.
178    */
179   protected void setTableRecordReader(TableRecordReader tableRecordReader) {
180     this.tableRecordReader = tableRecordReader;
181   }
182 
183   /**
184    * Allows subclasses to set the {@link Filter} to be used.
185    *
186    * @param rowFilter
187    */
188   protected void setRowFilter(Filter rowFilter) {
189     this.rowFilter = rowFilter;
190   }
191 }