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  import org.apache.hadoop.mapreduce.OutputFormat;
30  
31  /**
32   * Convenience class that simply writes all values (which must be
33   * {@link org.apache.hadoop.hbase.client.Put Put} or
34   * {@link org.apache.hadoop.hbase.client.Delete Delete} instances)
35   * passed to it out to the configured HBase table. This works in combination
36   * with {@link TableOutputFormat} which actually does the writing to HBase.<p>
37   *
38   * Keys are passed along but ignored in TableOutputFormat.  However, they can
39   * be used to control how your values will be divided up amongst the specified
40   * number of reducers. <p>
41   *
42   * You can also use the {@link TableMapReduceUtil} class to set up the two
43   * classes in one step:
44   * <blockquote><code>
45   * TableMapReduceUtil.initTableReducerJob("table", IdentityTableReducer.class, job);
46   * </code></blockquote>
47   * This will also set the proper {@link TableOutputFormat} which is given the
48   * <code>table</code> parameter. The
49   * {@link org.apache.hadoop.hbase.client.Put Put} or
50   * {@link org.apache.hadoop.hbase.client.Delete Delete} define the
51   * row and columns implicitly.
52   */
53  @InterfaceAudience.Public
54  @InterfaceStability.Stable
55  public class IdentityTableReducer
56  extends TableReducer<Writable, Mutation, Writable> {
57  
58    @SuppressWarnings("unused")
59    private static final Log LOG = LogFactory.getLog(IdentityTableReducer.class);
60  
61    /**
62     * Writes each given record, consisting of the row key and the given values,
63     * to the configured {@link OutputFormat}. 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 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 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  }