View Javadoc

1   /**
2    * Copyright 2007 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.mapreduce;
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.conf.Configurable;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HBaseConfiguration;
29  import org.apache.hadoop.hbase.client.HTable;
30  import org.apache.hadoop.hbase.client.Scan;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.apache.hadoop.util.StringUtils;
33  
34  /**
35   * Convert HBase tabular data into a format that is consumable by Map/Reduce.
36   */
37  public class TableInputFormat extends TableInputFormatBase
38  implements Configurable {
39  
40    private final Log LOG = LogFactory.getLog(TableInputFormat.class);
41  
42    /** Job parameter that specifies the input table. */
43    public static final String INPUT_TABLE = "hbase.mapreduce.inputtable";
44    /** Base-64 encoded scanner. All other SCAN_ confs are ignored if this is specified.
45     * See {@link TableMapReduceUtil#convertScanToString(Scan)} for more details.
46     */
47    public static final String SCAN = "hbase.mapreduce.scan";
48    /** Column Family to Scan */
49    public static final String SCAN_COLUMN_FAMILY = "hbase.mapreduce.scan.column.family";
50    /** Space delimited list of columns to scan. */
51    public static final String SCAN_COLUMNS = "hbase.mapreduce.scan.columns";
52    /** The timestamp used to filter columns with a specific timestamp. */
53    public static final String SCAN_TIMESTAMP = "hbase.mapreduce.scan.timestamp";
54    /** The starting timestamp used to filter columns with a specific range of versions. */
55    public static final String SCAN_TIMERANGE_START = "hbase.mapreduce.scan.timerange.start";
56    /** The ending timestamp used to filter columns with a specific range of versions. */
57    public static final String SCAN_TIMERANGE_END = "hbase.mapreduce.scan.timerange.end";
58    /** The maximum number of version to return. */
59    public static final String SCAN_MAXVERSIONS = "hbase.mapreduce.scan.maxversions";
60    /** Set to false to disable server-side caching of blocks for this scan. */
61    public static final String SCAN_CACHEBLOCKS = "hbase.mapreduce.scan.cacheblocks";
62    /** The number of rows for caching that will be passed to scanners. */
63    public static final String SCAN_CACHEDROWS = "hbase.mapreduce.scan.cachedrows";
64  
65    /** The configuration. */
66    private Configuration conf = null;
67  
68    /**
69     * Returns the current configuration.
70     *
71     * @return The current configuration.
72     * @see org.apache.hadoop.conf.Configurable#getConf()
73     */
74    @Override
75    public Configuration getConf() {
76      return conf;
77    }
78  
79    /**
80     * Sets the configuration. This is used to set the details for the table to
81     * be scanned.
82     *
83     * @param configuration  The configuration to set.
84     * @see org.apache.hadoop.conf.Configurable#setConf(
85     *   org.apache.hadoop.conf.Configuration)
86     */
87    @Override
88    public void setConf(Configuration configuration) {
89      this.conf = configuration;
90      String tableName = conf.get(INPUT_TABLE);
91      try {
92        setHTable(new HTable(HBaseConfiguration.create(conf), tableName));
93      } catch (Exception e) {
94        LOG.error(StringUtils.stringifyException(e));
95      }
96  
97      Scan scan = null;
98  
99      if (conf.get(SCAN) != null) {
100       try {
101         scan = TableMapReduceUtil.convertStringToScan(conf.get(SCAN));
102       } catch (IOException e) {
103         LOG.error("An error occurred.", e);
104       }
105     } else {
106       try {
107         scan = new Scan();
108 
109         if (conf.get(SCAN_COLUMNS) != null) {
110           scan.addColumns(conf.get(SCAN_COLUMNS));
111         }
112 
113         if (conf.get(SCAN_COLUMN_FAMILY) != null) {
114           scan.addFamily(Bytes.toBytes(conf.get(SCAN_COLUMN_FAMILY)));
115         }
116 
117         if (conf.get(SCAN_TIMESTAMP) != null) {
118           scan.setTimeStamp(Long.parseLong(conf.get(SCAN_TIMESTAMP)));
119         }
120 
121         if (conf.get(SCAN_TIMERANGE_START) != null && conf.get(SCAN_TIMERANGE_END) != null) {
122           scan.setTimeRange(
123               Long.parseLong(conf.get(SCAN_TIMERANGE_START)),
124               Long.parseLong(conf.get(SCAN_TIMERANGE_END)));
125         }
126 
127         if (conf.get(SCAN_MAXVERSIONS) != null) {
128           scan.setMaxVersions(Integer.parseInt(conf.get(SCAN_MAXVERSIONS)));
129         }
130 
131         if (conf.get(SCAN_CACHEDROWS) != null) {
132           scan.setCaching(Integer.parseInt(conf.get(SCAN_CACHEDROWS)));
133         }
134 
135         // false by default, full table scans generate too much BC churn
136         scan.setCacheBlocks((conf.getBoolean(SCAN_CACHEBLOCKS, false)));
137       } catch (Exception e) {
138           LOG.error(StringUtils.stringifyException(e));
139       }
140     }
141 
142     setScan(scan);
143   }
144 
145 }