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