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.KeyValue;
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    /** Scan start row */
49    public static final String SCAN_ROW_START = "hbase.mapreduce.scan.row.start";
50    /** Scan stop row */
51    public static final String SCAN_ROW_STOP = "hbase.mapreduce.scan.row.stop";
52    /** Column Family to Scan */
53    public static final String SCAN_COLUMN_FAMILY = "hbase.mapreduce.scan.column.family";
54    /** Space delimited list of columns to scan. */
55    public static final String SCAN_COLUMNS = "hbase.mapreduce.scan.columns";
56    /** The timestamp used to filter columns with a specific timestamp. */
57    public static final String SCAN_TIMESTAMP = "hbase.mapreduce.scan.timestamp";
58    /** The starting timestamp used to filter columns with a specific range of versions. */
59    public static final String SCAN_TIMERANGE_START = "hbase.mapreduce.scan.timerange.start";
60    /** The ending timestamp used to filter columns with a specific range of versions. */
61    public static final String SCAN_TIMERANGE_END = "hbase.mapreduce.scan.timerange.end";
62    /** The maximum number of version to return. */
63    public static final String SCAN_MAXVERSIONS = "hbase.mapreduce.scan.maxversions";
64    /** Set to false to disable server-side caching of blocks for this scan. */
65    public static final String SCAN_CACHEBLOCKS = "hbase.mapreduce.scan.cacheblocks";
66    /** The number of rows for caching that will be passed to scanners. */
67    public static final String SCAN_CACHEDROWS = "hbase.mapreduce.scan.cachedrows";
68  
69    /** The configuration. */
70    private Configuration conf = null;
71  
72    /**
73     * Returns the current configuration.
74     *
75     * @return The current configuration.
76     * @see org.apache.hadoop.conf.Configurable#getConf()
77     */
78    @Override
79    public Configuration getConf() {
80      return conf;
81    }
82  
83    /**
84     * Sets the configuration. This is used to set the details for the table to
85     * be scanned.
86     *
87     * @param configuration  The configuration to set.
88     * @see org.apache.hadoop.conf.Configurable#setConf(
89     *   org.apache.hadoop.conf.Configuration)
90     */
91    @Override
92    public void setConf(Configuration configuration) {
93      this.conf = configuration;
94      String tableName = conf.get(INPUT_TABLE);
95      try {
96        setHTable(new HTable(new Configuration(conf), tableName));
97      } catch (Exception e) {
98        LOG.error(StringUtils.stringifyException(e));
99      }
100 
101     Scan scan = null;
102 
103     if (conf.get(SCAN) != null) {
104       try {
105         scan = TableMapReduceUtil.convertStringToScan(conf.get(SCAN));
106       } catch (IOException e) {
107         LOG.error("An error occurred.", e);
108       }
109     } else {
110       try {
111         scan = new Scan();
112 
113         if (conf.get(SCAN_ROW_START) != null) {
114           scan.setStartRow(Bytes.toBytes(conf.get(SCAN_ROW_START)));
115         }
116 
117         if (conf.get(SCAN_ROW_STOP) != null) {
118           scan.setStopRow(Bytes.toBytes(conf.get(SCAN_ROW_STOP)));
119         }
120 
121         if (conf.get(SCAN_COLUMNS) != null) {
122           addColumns(scan, conf.get(SCAN_COLUMNS));
123         }
124 
125         if (conf.get(SCAN_COLUMN_FAMILY) != null) {
126           scan.addFamily(Bytes.toBytes(conf.get(SCAN_COLUMN_FAMILY)));
127         }
128 
129         if (conf.get(SCAN_TIMESTAMP) != null) {
130           scan.setTimeStamp(Long.parseLong(conf.get(SCAN_TIMESTAMP)));
131         }
132 
133         if (conf.get(SCAN_TIMERANGE_START) != null && conf.get(SCAN_TIMERANGE_END) != null) {
134           scan.setTimeRange(
135               Long.parseLong(conf.get(SCAN_TIMERANGE_START)),
136               Long.parseLong(conf.get(SCAN_TIMERANGE_END)));
137         }
138 
139         if (conf.get(SCAN_MAXVERSIONS) != null) {
140           scan.setMaxVersions(Integer.parseInt(conf.get(SCAN_MAXVERSIONS)));
141         }
142 
143         if (conf.get(SCAN_CACHEDROWS) != null) {
144           scan.setCaching(Integer.parseInt(conf.get(SCAN_CACHEDROWS)));
145         }
146 
147         // false by default, full table scans generate too much BC churn
148         scan.setCacheBlocks((conf.getBoolean(SCAN_CACHEBLOCKS, false)));
149       } catch (Exception e) {
150           LOG.error(StringUtils.stringifyException(e));
151       }
152     }
153 
154     setScan(scan);
155   }
156   
157   /**
158    * Parses a combined family and qualifier and adds either both or just the
159    * family in case there is not qualifier. This assumes the older colon
160    * divided notation, e.g. "data:contents" or "meta:".
161    * <p>
162    * Note: It will through an error when the colon is missing.
163    *
164    * @param familyAndQualifier family and qualifier
165    * @return A reference to this instance.
166    * @throws IllegalArgumentException When the colon is missing.
167    */
168   private static void addColumn(Scan scan, byte[] familyAndQualifier) {
169     byte [][] fq = KeyValue.parseColumn(familyAndQualifier);
170     if (fq.length > 1 && fq[1] != null && fq[1].length > 0) {
171       scan.addColumn(fq[0], fq[1]);
172     } else {
173       scan.addFamily(fq[0]);
174     }
175   }
176 
177   /**
178    * Adds an array of columns specified using old format, family:qualifier.
179    * <p>
180    * Overrides previous calls to addFamily for any families in the input.
181    *
182    * @param columns array of columns, formatted as <pre>family:qualifier</pre>
183    */
184   public static void addColumns(Scan scan, byte [][] columns) {
185     for (byte[] column : columns) {
186       addColumn(scan, column);
187     }
188   }
189 
190   /**
191    * Convenience method to help parse old style (or rather user entry on the
192    * command line) column definitions, e.g. "data:contents mime:". The columns
193    * must be space delimited and always have a colon (":") to denote family
194    * and qualifier.
195    *
196    * @param columns  The columns to parse.
197    * @return A reference to this instance.
198    */
199   private static void addColumns(Scan scan, String columns) {
200     String[] cols = columns.split(" ");
201     for (String col : cols) {
202       addColumn(scan, Bytes.toBytes(col));
203     }
204   }
205 
206 }