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  
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.hbase.client.Mutation;
28  import org.apache.hadoop.io.Writable;
29  
30  /**
31   * Convenience class that simply writes all values (which must be
32   * {@link org.apache.hadoop.hbase.client.Put} or
33   * {@link org.apache.hadoop.hbase.client.Delete} instances)
34   * passed to it out to the configured HBase table. This works in combination
35   * with {@link TableOutputFormat} which actually does the writing to HBase.<p>
36   *
37   * Keys are passed along but ignored in TableOutputFormat.  However, they can
38   * be used to control how your values will be divided up amongst the specified
39   * number of reducers. <p>
40   *
41   * You can also use the {@link TableMapReduceUtil} class to set up the two
42   * classes in one step:
43   * <blockquote><code>
44   * TableMapReduceUtil.initTableReducerJob("table", IdentityTableReducer.class, job);
45   * </code></blockquote>
46   * This will also set the proper {@link TableOutputFormat} which is given the
47   * <code>table</code> parameter. The
48   * {@link org.apache.hadoop.hbase.client.Put} or
49   * {@link org.apache.hadoop.hbase.client.Delete} define the
50   * row and columns implicitly.
51   */
52  @InterfaceAudience.Public
53  @InterfaceStability.Stable
54  public class IdentityTableReducer
55  extends TableReducer<Writable, Mutation, Writable> {
56  
57    @SuppressWarnings("unused")
58    private static final Log LOG = LogFactory.getLog(IdentityTableReducer.class);
59  
60    /**
61     * Writes each given record, consisting of the row key and the given values,
62     * to the configured {@link org.apache.hadoop.mapreduce.OutputFormat}. 
63     * It is emitting the row key and each
64     * {@link org.apache.hadoop.hbase.client.Put Put} or
65     * {@link org.apache.hadoop.hbase.client.Delete} as separate pairs.
66     *
67     * @param key  The current row key.
68     * @param values  The {@link org.apache.hadoop.hbase.client.Put Put} or
69     *   {@link org.apache.hadoop.hbase.client.Delete} list for the given
70     *   row.
71     * @param context  The context of the reduce.
72     * @throws IOException When writing the record fails.
73     * @throws InterruptedException When the job gets interrupted.
74     */
75    @Override
76    public void reduce(Writable key, Iterable<Mutation> values, Context context)
77    throws IOException, InterruptedException {
78      for(Mutation putOrDelete : values) {
79        context.write(key, putOrDelete);
80      }
81    }
82  }