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.classification.InterfaceAudience;
26  import org.apache.hadoop.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  @Deprecated
69  @InterfaceAudience.Public
70  @InterfaceStability.Stable
71  public abstract class TableInputFormatBase
72  implements InputFormat<ImmutableBytesWritable, Result> {
73    private static final Log LOG = LogFactory.getLog(TableInputFormatBase.class);
74    private byte [][] inputColumns;
75    private HTable table;
76    private TableRecordReader tableRecordReader;
77    private Filter rowFilter;
78  
79    /**
80     * Builds a TableRecordReader. If no TableRecordReader was provided, uses
81     * the default.
82     *
83     * @see org.apache.hadoop.mapred.InputFormat#getRecordReader(InputSplit,
84     *      JobConf, Reporter)
85     */
86    public RecordReader<ImmutableBytesWritable, Result> getRecordReader(
87        InputSplit split, JobConf job, Reporter reporter)
88    throws IOException {
89      TableSplit tSplit = (TableSplit) split;
90      TableRecordReader trr = this.tableRecordReader;
91      // if no table record reader was provided use default
92      if (trr == null) {
93        trr = new TableRecordReader();
94      }
95      trr.setStartRow(tSplit.getStartRow());
96      trr.setEndRow(tSplit.getEndRow());
97      trr.setHTable(this.table);
98      trr.setInputColumns(this.inputColumns);
99      trr.setRowFilter(this.rowFilter);
100     trr.init();
101     return trr;
102   }
103 
104   /**
105    * Calculates the splits that will serve as input for the map tasks.
106    * <ul>
107    * Splits are created in number equal to the smallest between numSplits and
108    * the number of {@link HRegion}s in the table. If the number of splits is
109    * smaller than the number of {@link HRegion}s then splits are spanned across
110    * multiple {@link HRegion}s and are grouped the most evenly possible. In the
111    * case splits are uneven the bigger splits are placed first in the
112    * {@link InputSplit} array.
113    *
114    * @param job the map task {@link JobConf}
115    * @param numSplits a hint to calculate the number of splits (mapred.map.tasks).
116    *
117    * @return the input splits
118    *
119    * @see org.apache.hadoop.mapred.InputFormat#getSplits(org.apache.hadoop.mapred.JobConf, int)
120    */
121   public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException {
122     if (this.table == null) {
123       throw new IOException("No table was provided");
124     }
125     byte [][] startKeys = this.table.getStartKeys();
126     if (startKeys == null || startKeys.length == 0) {
127       throw new IOException("Expecting at least one region");
128     }
129     if (this.inputColumns == null || this.inputColumns.length == 0) {
130       throw new IOException("Expecting at least one column");
131     }
132     int realNumSplits = numSplits > startKeys.length? startKeys.length:
133       numSplits;
134     InputSplit[] splits = new InputSplit[realNumSplits];
135     int middle = startKeys.length / realNumSplits;
136     int startPos = 0;
137     for (int i = 0; i < realNumSplits; i++) {
138       int lastPos = startPos + middle;
139       lastPos = startKeys.length % realNumSplits > i ? lastPos + 1 : lastPos;
140       String regionLocation = table.getRegionLocation(startKeys[startPos]).
141         getHostname();
142       splits[i] = new TableSplit(this.table.getName(),
143         startKeys[startPos], ((i + 1) < realNumSplits) ? startKeys[lastPos]:
144           HConstants.EMPTY_START_ROW, regionLocation);
145       LOG.info("split: " + i + "->" + splits[i]);
146       startPos = lastPos;
147     }
148     return splits;
149   }
150 
151   /**
152    * @param inputColumns to be passed in {@link Result} to the map task.
153    */
154   protected void setInputColumns(byte [][] inputColumns) {
155     this.inputColumns = inputColumns;
156   }
157 
158   /**
159    * Allows subclasses to get the {@link HTable}.
160    */
161   protected HTable getHTable() {
162     return this.table;
163   }
164 
165   /**
166    * Allows subclasses to set the {@link HTable}.
167    *
168    * @param table to get the data from
169    */
170   protected void setHTable(HTable table) {
171     this.table = table;
172   }
173 
174   /**
175    * Allows subclasses to set the {@link TableRecordReader}.
176    *
177    * @param tableRecordReader
178    *                to provide other {@link TableRecordReader} implementations.
179    */
180   protected void setTableRecordReader(TableRecordReader tableRecordReader) {
181     this.tableRecordReader = tableRecordReader;
182   }
183 
184   /**
185    * Allows subclasses to set the {@link Filter} to be used.
186    *
187    * @param rowFilter
188    */
189   protected void setRowFilter(Filter rowFilter) {
190     this.rowFilter = rowFilter;
191   }
192 }