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.mapreduce;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.classification.InterfaceAudience;
28  import org.apache.hadoop.classification.InterfaceStability;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.KeyValue;
31  import org.apache.hadoop.hbase.client.HTable;
32  import org.apache.hadoop.hbase.client.Table;
33  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
34  import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
35  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
36  import org.apache.hadoop.hbase.regionserver.BloomType;
37  import org.apache.hadoop.mapreduce.Job;
38  import org.apache.hadoop.mapreduce.RecordWriter;
39  import org.apache.hadoop.mapreduce.TaskAttemptContext;
40  import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
41  
42  import com.google.common.annotations.VisibleForTesting;
43  
44  /**
45   * Writes HFiles. Passed KeyValues must arrive in order.
46   * Writes current time as the sequence id for the file. Sets the major compacted
47   * attribute on created hfiles. Calling write(null,null) will forcibly roll
48   * all HFiles being written.
49   * <p>
50   * Using this class as part of a MapReduce job is best done
51   * using {@link #configureIncrementalLoad(Job, HTable)}.
52   * @see KeyValueSortReducer
53   * @deprecated use {@link HFileOutputFormat2} instead.
54   */
55  @Deprecated
56  @InterfaceAudience.Public
57  @InterfaceStability.Stable
58  public class HFileOutputFormat extends FileOutputFormat<ImmutableBytesWritable, KeyValue> {
59    static Log LOG = LogFactory.getLog(HFileOutputFormat.class);
60  
61    // This constant is public since the client can modify this when setting
62    // up their conf object and thus refer to this symbol.
63    // It is present for backwards compatibility reasons. Use it only to
64    // override the auto-detection of datablock encoding.
65    public static final String DATABLOCK_ENCODING_OVERRIDE_CONF_KEY =
66      HFileOutputFormat2.DATABLOCK_ENCODING_OVERRIDE_CONF_KEY;
67  
68    public RecordWriter<ImmutableBytesWritable, KeyValue> getRecordWriter(
69        final TaskAttemptContext context) throws IOException, InterruptedException {
70      return HFileOutputFormat2.createRecordWriter(context);
71    }
72  
73    /**
74     * Configure a MapReduce Job to perform an incremental load into the given
75     * table. This
76     * <ul>
77     *   <li>Inspects the table to configure a total order partitioner</li>
78     *   <li>Uploads the partitions file to the cluster and adds it to the DistributedCache</li>
79     *   <li>Sets the number of reduce tasks to match the current number of regions</li>
80     *   <li>Sets the output key/value class to match HFileOutputFormat's requirements</li>
81     *   <li>Sets the reducer up to perform the appropriate sorting (either KeyValueSortReducer or
82     *     PutSortReducer)</li>
83     * </ul>
84     * The user should be sure to set the map output value class to either KeyValue or Put before
85     * running this function.
86     */
87    public static void configureIncrementalLoad(Job job, HTable table)
88        throws IOException {
89      HFileOutputFormat2.configureIncrementalLoad(job, table, HFileOutputFormat.class);
90    }
91  
92    /**
93     * Runs inside the task to deserialize column family to compression algorithm
94     * map from the configuration.
95     *
96     * @param conf to read the serialized values from
97     * @return a map from column family to the configured compression algorithm
98     */
99    @VisibleForTesting
100   static Map<byte[], Algorithm> createFamilyCompressionMap(Configuration
101       conf) {
102     return HFileOutputFormat2.createFamilyCompressionMap(conf);
103   }
104 
105   /**
106    * Runs inside the task to deserialize column family to bloom filter type
107    * map from the configuration.
108    *
109    * @param conf to read the serialized values from
110    * @return a map from column family to the the configured bloom filter type
111    */
112   @VisibleForTesting
113   static Map<byte[], BloomType> createFamilyBloomTypeMap(Configuration conf) {
114     return HFileOutputFormat2.createFamilyBloomTypeMap(conf);
115   }
116 
117   /**
118    * Runs inside the task to deserialize column family to block size
119    * map from the configuration.
120    *
121    * @param conf to read the serialized values from
122    * @return a map from column family to the configured block size
123    */
124   @VisibleForTesting
125   static Map<byte[], Integer> createFamilyBlockSizeMap(Configuration conf) {
126     return HFileOutputFormat2.createFamilyBlockSizeMap(conf);
127   }
128 
129   /**
130    * Runs inside the task to deserialize column family to data block encoding
131    * type map from the configuration.
132    *
133    * @param conf to read the serialized values from
134    * @return a map from column family to HFileDataBlockEncoder for the
135    *         configured data block type for the family
136    */
137   @VisibleForTesting
138   static Map<byte[], DataBlockEncoding> createFamilyDataBlockEncodingMap(
139       Configuration conf) {
140     return HFileOutputFormat2.createFamilyDataBlockEncodingMap(conf);
141   }
142 
143   /**
144    * Configure <code>job</code> with a TotalOrderPartitioner, partitioning against
145    * <code>splitPoints</code>. Cleans up the partitions file after job exists.
146    */
147   static void configurePartitioner(Job job, List<ImmutableBytesWritable> splitPoints)
148       throws IOException {
149     HFileOutputFormat2.configurePartitioner(job, splitPoints);
150   }
151 
152   /**
153    * Serialize column family to compression algorithm map to configuration.
154    * Invoked while configuring the MR job for incremental load.
155    *
156    * @param table to read the properties from
157    * @param conf to persist serialized values into
158    * @throws IOException
159    *           on failure to read column family descriptors
160    */
161   @edu.umd.cs.findbugs.annotations.SuppressWarnings(
162       value="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
163   @VisibleForTesting
164   static void configureCompression(Table table, Configuration conf) throws IOException {
165     HFileOutputFormat2.configureCompression(table, conf);
166   }
167 
168   /**
169    * Serialize column family to block size map to configuration.
170    * Invoked while configuring the MR job for incremental load.
171    *
172    * @param table to read the properties from
173    * @param conf to persist serialized values into
174    * @throws IOException
175    *           on failure to read column family descriptors
176    */
177   @VisibleForTesting
178   static void configureBlockSize(Table table, Configuration conf) throws IOException {
179     HFileOutputFormat2.configureBlockSize(table, conf);
180   }
181 
182   /**
183    * Serialize column family to bloom type map to configuration.
184    * Invoked while configuring the MR job for incremental load.
185    *
186    * @param table to read the properties from
187    * @param conf to persist serialized values into
188    * @throws IOException
189    *           on failure to read column family descriptors
190    */
191   @VisibleForTesting
192   static void configureBloomType(Table table, Configuration conf) throws IOException {
193     HFileOutputFormat2.configureBloomType(table, conf);
194   }
195 
196   /**
197    * Serialize column family to data block encoding map to configuration.
198    * Invoked while configuring the MR job for incremental load.
199    *
200    * @param table to read the properties from
201    * @param conf to persist serialized values into
202    * @throws IOException
203    *           on failure to read column family descriptors
204    */
205   @VisibleForTesting
206   static void configureDataBlockEncoding(Table table,
207       Configuration conf) throws IOException {
208     HFileOutputFormat2.configureDataBlockEncoding(table, conf);
209   }
210 }