001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.mapreduce; 020 021import java.io.IOException; 022 023import org.apache.yetus.audience.InterfaceAudience; 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026import org.apache.hadoop.hbase.client.Mutation; 027import org.apache.hadoop.io.Writable; 028 029/** 030 * Convenience class that simply writes all values (which must be 031 * {@link org.apache.hadoop.hbase.client.Put Put} or 032 * {@link org.apache.hadoop.hbase.client.Delete Delete} instances) 033 * passed to it out to the configured HBase table. This works in combination 034 * with {@link TableOutputFormat} which actually does the writing to HBase.<p> 035 * 036 * Keys are passed along but ignored in TableOutputFormat. However, they can 037 * be used to control how your values will be divided up amongst the specified 038 * number of reducers. <p> 039 * 040 * You can also use the {@link TableMapReduceUtil} class to set up the two 041 * classes in one step: 042 * <blockquote><code> 043 * TableMapReduceUtil.initTableReducerJob("table", IdentityTableReducer.class, job); 044 * </code></blockquote> 045 * This will also set the proper {@link TableOutputFormat} which is given the 046 * <code>table</code> parameter. The 047 * {@link org.apache.hadoop.hbase.client.Put Put} or 048 * {@link org.apache.hadoop.hbase.client.Delete Delete} define the 049 * row and columns implicitly. 050 */ 051@InterfaceAudience.Public 052public class IdentityTableReducer 053extends TableReducer<Writable, Mutation, Writable> { 054 055 @SuppressWarnings("unused") 056 private static final Logger LOG = LoggerFactory.getLogger(IdentityTableReducer.class); 057 058 /** 059 * Writes each given record, consisting of the row key and the given values, 060 * to the configured {@link org.apache.hadoop.mapreduce.OutputFormat}. 061 * It is emitting the row key and each {@link org.apache.hadoop.hbase.client.Put Put} 062 * or {@link org.apache.hadoop.hbase.client.Delete Delete} as separate pairs. 063 * 064 * @param key The current row key. 065 * @param values The {@link org.apache.hadoop.hbase.client.Put Put} or 066 * {@link org.apache.hadoop.hbase.client.Delete Delete} list for the given 067 * row. 068 * @param context The context of the reduce. 069 * @throws IOException When writing the record fails. 070 * @throws InterruptedException When the job gets interrupted. 071 */ 072 @Override 073 public void reduce(Writable key, Iterable<Mutation> values, Context context) 074 throws IOException, InterruptedException { 075 for(Mutation putOrDelete : values) { 076 context.write(key, putOrDelete); 077 } 078 } 079}