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