View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.mapreduce;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.client.HTable;
29  import org.apache.hadoop.hbase.client.Result;
30  import org.apache.hadoop.hbase.client.Scan;
31  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.apache.hadoop.hbase.util.Pair;
34  import org.apache.hadoop.mapreduce.InputFormat;
35  import org.apache.hadoop.mapreduce.InputSplit;
36  import org.apache.hadoop.mapreduce.JobContext;
37  import org.apache.hadoop.mapreduce.RecordReader;
38  import org.apache.hadoop.mapreduce.TaskAttemptContext;
39  
40  /**
41   * A base for {@link MultiTableInputFormat}s. Receives a list of
42   * {@link Scan} instances that define the input tables and
43   * filters etc. Subclasses may use other TableRecordReader implementations.
44   */
45  @InterfaceAudience.Public
46  @InterfaceStability.Evolving
47  public abstract class MultiTableInputFormatBase extends
48      InputFormat<ImmutableBytesWritable, Result> {
49  
50    final Log LOG = LogFactory.getLog(MultiTableInputFormatBase.class);
51  
52    /** Holds the set of scans used to define the input. */
53    private List<Scan> scans;
54  
55    /** The reader scanning the table, can be a custom one. */
56    private TableRecordReader tableRecordReader = null;
57  
58    /**
59     * Builds a TableRecordReader. If no TableRecordReader was provided, uses the
60     * default.
61     *
62     * @param split The split to work with.
63     * @param context The current context.
64     * @return The newly created record reader.
65     * @throws IOException When creating the reader fails.
66     * @throws InterruptedException when record reader initialization fails
67     * @see org.apache.hadoop.mapreduce.InputFormat#createRecordReader(
68     *      org.apache.hadoop.mapreduce.InputSplit,
69     *      org.apache.hadoop.mapreduce.TaskAttemptContext)
70     */
71    @Override
72    public RecordReader<ImmutableBytesWritable, Result> createRecordReader(
73        InputSplit split, TaskAttemptContext context)
74        throws IOException, InterruptedException {
75      TableSplit tSplit = (TableSplit) split;
76  
77      if (tSplit.getTableName() == null) {
78        throw new IOException("Cannot create a record reader because of a"
79            + " previous error. Please look at the previous logs lines from"
80            + " the task's full log for more details.");
81      }
82      HTable table =
83          new HTable(context.getConfiguration(), tSplit.getTableName());
84  
85      TableRecordReader trr = this.tableRecordReader;
86  
87      try {
88        // if no table record reader was provided use default
89        if (trr == null) {
90          trr = new TableRecordReader();
91        }
92        Scan sc = tSplit.getScan();
93        sc.setStartRow(tSplit.getStartRow());
94        sc.setStopRow(tSplit.getEndRow());
95        trr.setScan(sc);
96        trr.setHTable(table);
97      } catch (IOException ioe) {
98        // If there is an exception make sure that all
99        // resources are closed and released.
100       table.close();
101       trr.close();
102       throw ioe;
103     }
104     return trr;
105   }
106 
107   /**
108    * Calculates the splits that will serve as input for the map tasks. The
109    * number of splits matches the number of regions in a table.
110    *
111    * @param context The current job context.
112    * @return The list of input splits.
113    * @throws IOException When creating the list of splits fails.
114    * @see org.apache.hadoop.mapreduce.InputFormat#getSplits(org.apache.hadoop.mapreduce.JobContext)
115    */
116   @Override
117   public List<InputSplit> getSplits(JobContext context) throws IOException {
118     if (scans.isEmpty()) {
119       throw new IOException("No scans were provided.");
120     }
121     List<InputSplit> splits = new ArrayList<InputSplit>();
122 
123     for (Scan scan : scans) {
124       byte[] tableName = scan.getAttribute(Scan.SCAN_ATTRIBUTES_TABLE_NAME);
125       if (tableName == null) 
126         throw new IOException("A scan object did not have a table name");
127 
128       HTable table = null;
129       try {
130         table = new HTable(context.getConfiguration(), tableName);
131         Pair<byte[][], byte[][]> keys = table.getStartEndKeys();
132         if (keys == null || keys.getFirst() == null ||
133             keys.getFirst().length == 0) {
134           throw new IOException("Expecting at least one region for table : "
135               + Bytes.toString(tableName));
136         }
137         int count = 0;
138 
139         byte[] startRow = scan.getStartRow();
140         byte[] stopRow = scan.getStopRow();
141 
142         for (int i = 0; i < keys.getFirst().length; i++) {
143           if (!includeRegionInSplit(keys.getFirst()[i], keys.getSecond()[i])) {
144             continue;
145           }
146           String regionLocation =
147               table.getRegionLocation(keys.getFirst()[i], false).getHostname();
148         
149           // determine if the given start and stop keys fall into the range
150           if ((startRow.length == 0 || keys.getSecond()[i].length == 0 ||
151               Bytes.compareTo(startRow, keys.getSecond()[i]) < 0) &&
152               (stopRow.length == 0 ||
153                   Bytes.compareTo(stopRow, keys.getFirst()[i]) > 0)) {
154             byte[] splitStart =
155                 startRow.length == 0 ||
156                     Bytes.compareTo(keys.getFirst()[i], startRow) >= 0 ? keys
157                     .getFirst()[i] : startRow;
158             byte[] splitStop =
159                 (stopRow.length == 0 || Bytes.compareTo(keys.getSecond()[i],
160                     stopRow) <= 0) && keys.getSecond()[i].length > 0 ? keys
161                     .getSecond()[i] : stopRow;
162             InputSplit split =
163                 new TableSplit(table.getName(),
164                     scan, splitStart, splitStop, regionLocation);
165             splits.add(split);
166             if (LOG.isDebugEnabled())
167               LOG.debug("getSplits: split -> " + (count++) + " -> " + split);
168           }
169         }
170       } finally {
171         if (null != table) table.close();
172       }
173     }
174     return splits;
175   }
176 
177   /**
178    * Test if the given region is to be included in the InputSplit while
179    * splitting the regions of a table.
180    * <p>
181    * This optimization is effective when there is a specific reasoning to
182    * exclude an entire region from the M-R job, (and hence, not contributing to
183    * the InputSplit), given the start and end keys of the same. <br>
184    * Useful when we need to remember the last-processed top record and revisit
185    * the [last, current) interval for M-R processing, continuously. In addition
186    * to reducing InputSplits, reduces the load on the region server as well, due
187    * to the ordering of the keys. <br>
188    * <br>
189    * Note: It is possible that <code>endKey.length() == 0 </code> , for the last
190    * (recent) region. <br>
191    * Override this method, if you want to bulk exclude regions altogether from
192    * M-R. By default, no region is excluded( i.e. all regions are included).
193    *
194    * @param startKey Start key of the region
195    * @param endKey End key of the region
196    * @return true, if this region needs to be included as part of the input
197    *         (default).
198    */
199   protected boolean includeRegionInSplit(final byte[] startKey,
200       final byte[] endKey) {
201     return true;
202   }
203 
204   /**
205    * Allows subclasses to get the list of {@link Scan} objects.
206    */
207   protected List<Scan> getScans() {
208     return this.scans;
209   }
210 
211   /**
212    * Allows subclasses to set the list of {@link Scan} objects.
213    *
214    * @param scans The list of {@link Scan} used to define the input
215    */
216   protected void setScans(List<Scan> scans) {
217     this.scans = scans;
218   }
219 
220   /**
221    * Allows subclasses to set the {@link TableRecordReader}.
222    *
223    * @param tableRecordReader A different {@link TableRecordReader}
224    *          implementation.
225    */
226   protected void setTableRecordReader(TableRecordReader tableRecordReader) {
227     this.tableRecordReader = tableRecordReader;
228   }
229 }