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.fs.Path;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.client.Connection;
30  import org.apache.hadoop.hbase.client.ConnectionFactory;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.apache.hadoop.mapred.FileInputFormat;
33  import org.apache.hadoop.mapred.JobConf;
34  import org.apache.hadoop.mapred.JobConfigurable;
35  import org.apache.hadoop.util.StringUtils;
36  
37  /**
38   * Convert HBase tabular data into a format that is consumable by Map/Reduce.
39   */
40  @InterfaceAudience.Public
41  @InterfaceStability.Stable
42  public class TableInputFormat extends TableInputFormatBase implements
43      JobConfigurable {
44    private static final Log LOG = LogFactory.getLog(TableInputFormat.class);
45  
46    /**
47     * space delimited list of columns
48     */
49    public static final String COLUMN_LIST = "hbase.mapred.tablecolumns";
50  
51    public void configure(JobConf job) {
52      try {
53        initialize(job);
54      } catch (Exception e) {
55        LOG.error(StringUtils.stringifyException(e));
56      }
57    }
58  
59    @Override
60    protected void initialize(JobConf job) throws IOException {
61      Path[] tableNames = FileInputFormat.getInputPaths(job);
62      String colArg = job.get(COLUMN_LIST);
63      String[] colNames = colArg.split(" ");
64      byte [][] m_cols = new byte[colNames.length][];
65      for (int i = 0; i < m_cols.length; i++) {
66        m_cols[i] = Bytes.toBytes(colNames[i]);
67      }
68      setInputColumns(m_cols);
69      Connection connection = ConnectionFactory.createConnection(job);
70      initializeTable(connection, TableName.valueOf(tableNames[0].getName()));
71    }
72  
73    public void validateInput(JobConf job) throws IOException {
74      // expecting exactly one path
75      Path [] tableNames = FileInputFormat.getInputPaths(job);
76      if (tableNames == null || tableNames.length > 1) {
77        throw new IOException("expecting one table name");
78      }
79  
80      // connected to table?
81      if (getHTable() == null) {
82        throw new IOException("could not connect to table '" +
83          tableNames[0].getName() + "'");
84      }
85  
86      // expecting at least one column
87      String colArg = job.get(COLUMN_LIST);
88      if (colArg == null || colArg.length() == 0) {
89        throw new IOException("expecting at least one column");
90      }
91    }
92  }