View Javadoc

1   /**
2    * Copyright 2009 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.conf.Configurable;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HBaseConfiguration;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.client.Delete;
31  import org.apache.hadoop.hbase.client.HTable;
32  import org.apache.hadoop.hbase.client.Put;
33  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
34  import org.apache.hadoop.io.Writable;
35  import org.apache.hadoop.mapreduce.JobContext;
36  import org.apache.hadoop.mapreduce.OutputCommitter;
37  import org.apache.hadoop.mapreduce.OutputFormat;
38  import org.apache.hadoop.mapreduce.RecordWriter;
39  import org.apache.hadoop.mapreduce.TaskAttemptContext;
40  
41  /**
42   * Convert Map/Reduce output and write it to an HBase table. The KEY is ignored
43   * while the output value <u>must</u> be either a {@link Put} or a
44   * {@link Delete} instance.
45   *
46   * @param <KEY>  The type of the key. Ignored in this class.
47   */
48  public class TableOutputFormat<KEY> extends OutputFormat<KEY, Writable>
49  implements Configurable {
50  
51    private final Log LOG = LogFactory.getLog(TableOutputFormat.class);
52  
53    /** Job parameter that specifies the output table. */
54    public static final String OUTPUT_TABLE = "hbase.mapred.outputtable";
55  
56    /**
57     * Optional job parameter to specify a peer cluster.
58     * Used specifying remote cluster when copying between hbase clusters (the
59     * source is picked up from <code>hbase-site.xml</code>).
60     * @see TableMapReduceUtil#initTableReducerJob(String, Class, org.apache.hadoop.mapreduce.Job, Class, String, String, String)
61     */
62    public static final String QUORUM_ADDRESS = "hbase.mapred.output.quorum";
63  
64    /** Optional job parameter to specify peer cluster's ZK client port */
65    public static final String QUORUM_PORT = "hbase.mapred.output.quorum.port";
66  
67    /** Optional specification of the rs class name of the peer cluster */
68    public static final String
69        REGION_SERVER_CLASS = "hbase.mapred.output.rs.class";
70    /** Optional specification of the rs impl name of the peer cluster */
71    public static final String
72        REGION_SERVER_IMPL = "hbase.mapred.output.rs.impl";
73  
74    /** The configuration. */
75    private Configuration conf = null;
76  
77    private HTable table;
78  
79    /**
80     * Writes the reducer output to an HBase table.
81     *
82     * @param <KEY>  The type of the key.
83     */
84    protected static class TableRecordWriter<KEY>
85    extends RecordWriter<KEY, Writable> {
86  
87      /** The table to write to. */
88      private HTable table;
89  
90      /**
91       * Instantiate a TableRecordWriter with the HBase HClient for writing.
92       *
93       * @param table  The table to write to.
94       */
95      public TableRecordWriter(HTable table) {
96        this.table = table;
97      }
98  
99      /**
100      * Closes the writer, in this case flush table commits.
101      *
102      * @param context  The context.
103      * @throws IOException When closing the writer fails.
104      * @see org.apache.hadoop.mapreduce.RecordWriter#close(org.apache.hadoop.mapreduce.TaskAttemptContext)
105      */
106     @Override
107     public void close(TaskAttemptContext context)
108     throws IOException {
109       table.close();
110     }
111 
112     /**
113      * Writes a key/value pair into the table.
114      *
115      * @param key  The key.
116      * @param value  The value.
117      * @throws IOException When writing fails.
118      * @see org.apache.hadoop.mapreduce.RecordWriter#write(java.lang.Object, java.lang.Object)
119      */
120     @Override
121     public void write(KEY key, Writable value)
122     throws IOException {
123       if (value instanceof Put) this.table.put(new Put((Put)value));
124       else if (value instanceof Delete) this.table.delete(new Delete((Delete)value));
125       else throw new IOException("Pass a Delete or a Put");
126     }
127   }
128 
129   /**
130    * Creates a new record writer.
131    *
132    * @param context  The current task context.
133    * @return The newly created writer instance.
134    * @throws IOException When creating the writer fails.
135    * @throws InterruptedException When the jobs is cancelled.
136    * @see org.apache.hadoop.mapreduce.lib.output.FileOutputFormat#getRecordWriter(org.apache.hadoop.mapreduce.TaskAttemptContext)
137    */
138   @Override
139   public RecordWriter<KEY, Writable> getRecordWriter(
140     TaskAttemptContext context)
141   throws IOException, InterruptedException {
142     return new TableRecordWriter<KEY>(this.table);
143   }
144 
145   /**
146    * Checks if the output target exists.
147    *
148    * @param context  The current context.
149    * @throws IOException When the check fails.
150    * @throws InterruptedException When the job is aborted.
151    * @see org.apache.hadoop.mapreduce.OutputFormat#checkOutputSpecs(org.apache.hadoop.mapreduce.JobContext)
152    */
153   @Override
154   public void checkOutputSpecs(JobContext context) throws IOException,
155       InterruptedException {
156     // TODO Check if the table exists?
157 
158   }
159 
160   /**
161    * Returns the output committer.
162    *
163    * @param context  The current context.
164    * @return The committer.
165    * @throws IOException When creating the committer fails.
166    * @throws InterruptedException When the job is aborted.
167    * @see org.apache.hadoop.mapreduce.OutputFormat#getOutputCommitter(org.apache.hadoop.mapreduce.TaskAttemptContext)
168    */
169   @Override
170   public OutputCommitter getOutputCommitter(TaskAttemptContext context)
171   throws IOException, InterruptedException {
172     return new TableOutputCommitter();
173   }
174 
175   public Configuration getConf() {
176     return conf;
177   }
178 
179   @Override
180   public void setConf(Configuration otherConf) {
181     this.conf = HBaseConfiguration.create(otherConf);
182     String tableName = this.conf.get(OUTPUT_TABLE);
183     if(tableName == null || tableName.length() <= 0) {
184       throw new IllegalArgumentException("Must specify table name");
185     }
186     String address = this.conf.get(QUORUM_ADDRESS);
187     int zkClientPort = conf.getInt(QUORUM_PORT, 0);
188     String serverClass = this.conf.get(REGION_SERVER_CLASS);
189     String serverImpl = this.conf.get(REGION_SERVER_IMPL);
190     try {
191       if (address != null) {
192         ZKUtil.applyClusterKeyToConf(this.conf, address);
193       }
194       if (serverClass != null) {
195         this.conf.set(HConstants.REGION_SERVER_CLASS, serverClass);
196         this.conf.set(HConstants.REGION_SERVER_IMPL, serverImpl);
197       }
198       if (zkClientPort != 0) {
199         conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, zkClientPort);
200       }
201       this.table = new HTable(this.conf, tableName);
202       this.table.setAutoFlush(false);
203       LOG.info("Created table instance for "  + tableName);
204     } catch(IOException e) {
205       LOG.error(e);
206       throw new RuntimeException(e);
207     }
208   }
209 }