View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.mapred;
21  
22  import java.io.IOException;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.hbase.HBaseConfiguration;
28  import org.apache.hadoop.hbase.client.HTable;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.apache.hadoop.mapred.FileInputFormat;
31  import org.apache.hadoop.mapred.JobConf;
32  import org.apache.hadoop.mapred.JobConfigurable;
33  import org.apache.hadoop.util.StringUtils;
34  
35  /**
36   * Convert HBase tabular data into a format that is consumable by Map/Reduce.
37   */
38  @Deprecated
39  public class TableInputFormat extends TableInputFormatBase implements
40      JobConfigurable {
41    private final Log LOG = LogFactory.getLog(TableInputFormat.class);
42  
43    /**
44     * space delimited list of columns
45     */
46    public static final String COLUMN_LIST = "hbase.mapred.tablecolumns";
47  
48    public void configure(JobConf job) {
49      Path[] tableNames = FileInputFormat.getInputPaths(job);
50      String colArg = job.get(COLUMN_LIST);
51      String[] colNames = colArg.split(" ");
52      byte [][] m_cols = new byte[colNames.length][];
53      for (int i = 0; i < m_cols.length; i++) {
54        m_cols[i] = Bytes.toBytes(colNames[i]);
55      }
56      setInputColumns(m_cols);
57      try {
58        setHTable(new HTable(HBaseConfiguration.create(job), tableNames[0].getName()));
59      } catch (Exception e) {
60        LOG.error(StringUtils.stringifyException(e));
61      }
62    }
63  
64    public void validateInput(JobConf job) throws IOException {
65      // expecting exactly one path
66      Path [] tableNames = FileInputFormat.getInputPaths(job);
67      if (tableNames == null || tableNames.length > 1) {
68        throw new IOException("expecting one table name");
69      }
70  
71      // connected to table?
72      if (getHTable() == null) {
73        throw new IOException("could not connect to table '" +
74          tableNames[0].getName() + "'");
75      }
76  
77      // expecting at least one column
78      String colArg = job.get(COLUMN_LIST);
79      if (colArg == null || colArg.length() == 0) {
80        throw new IOException("expecting at least one column");
81      }
82    }
83  }