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