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.classification.InterfaceAudience;
26  import org.apache.hadoop.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.client.Delete;
32  import org.apache.hadoop.hbase.client.HTable;
33  import org.apache.hadoop.hbase.client.Mutation;
34  import org.apache.hadoop.hbase.client.Put;
35  import org.apache.hadoop.hbase.client.Table;
36  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
37  import org.apache.hadoop.mapreduce.JobContext;
38  import org.apache.hadoop.mapreduce.OutputCommitter;
39  import org.apache.hadoop.mapreduce.OutputFormat;
40  import org.apache.hadoop.mapreduce.RecordWriter;
41  import org.apache.hadoop.mapreduce.TaskAttemptContext;
42  
43  /**
44   * Convert Map/Reduce output and write it to an HBase table. The KEY is ignored
45   * while the output value <u>must</u> be either a {@link Put} or a
46   * {@link Delete} instance.
47   *
48   * @param <KEY>  The type of the key. Ignored in this class.
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 HTable table;
82  
83    /**
84     * Writes the reducer output to an HBase table.
85     *
86     * @param <KEY>  The type of the key.
87     */
88    protected static class TableRecordWriter<KEY>
89    extends RecordWriter<KEY, Mutation> {
90  
91      /** The table to write to. */
92      private Table table;
93  
94      /**
95       * Instantiate a TableRecordWriter with the HBase HClient for writing.
96       *
97       * @param table  The table to write to.
98       */
99      public TableRecordWriter(Table table) {
100       this.table = table;
101     }
102 
103     /**
104      * Closes the writer, in this case flush table commits.
105      *
106      * @param context  The context.
107      * @throws IOException When closing the writer fails.
108      * @see org.apache.hadoop.mapreduce.RecordWriter#close(org.apache.hadoop.mapreduce.TaskAttemptContext)
109      */
110     @Override
111     public void close(TaskAttemptContext context)
112     throws IOException {
113       table.close();
114     }
115 
116     /**
117      * Writes a key/value pair into the table.
118      *
119      * @param key  The key.
120      * @param value  The value.
121      * @throws IOException When writing fails.
122      * @see org.apache.hadoop.mapreduce.RecordWriter#write(java.lang.Object, java.lang.Object)
123      */
124     @Override
125     public void write(KEY key, Mutation value)
126     throws IOException {
127       if (value instanceof Put) this.table.put(new Put((Put)value));
128       else if (value instanceof Delete) this.table.delete(new Delete((Delete)value));
129       else throw new IOException("Pass a Delete or a Put");
130     }
131   }
132 
133   /**
134    * Creates a new record writer.
135    *
136    * @param context  The current task context.
137    * @return The newly created writer instance.
138    * @throws IOException When creating the writer fails.
139    * @throws InterruptedException When the jobs is cancelled.
140    * @see org.apache.hadoop.mapreduce.lib.output.FileOutputFormat#getRecordWriter(org.apache.hadoop.mapreduce.TaskAttemptContext)
141    */
142   @Override
143   public RecordWriter<KEY, Mutation> getRecordWriter(
144     TaskAttemptContext context)
145   throws IOException, InterruptedException {
146     return new TableRecordWriter<KEY>(this.table);
147   }
148 
149   /**
150    * Checks if the output target exists.
151    *
152    * @param context  The current context.
153    * @throws IOException When the check fails.
154    * @throws InterruptedException When the job is aborted.
155    * @see org.apache.hadoop.mapreduce.OutputFormat#checkOutputSpecs(org.apache.hadoop.mapreduce.JobContext)
156    */
157   @Override
158   public void checkOutputSpecs(JobContext context) throws IOException,
159       InterruptedException {
160     // TODO Check if the table exists?
161 
162   }
163 
164   /**
165    * Returns the output committer.
166    *
167    * @param context  The current context.
168    * @return The committer.
169    * @throws IOException When creating the committer fails.
170    * @throws InterruptedException When the job is aborted.
171    * @see org.apache.hadoop.mapreduce.OutputFormat#getOutputCommitter(org.apache.hadoop.mapreduce.TaskAttemptContext)
172    */
173   @Override
174   public OutputCommitter getOutputCommitter(TaskAttemptContext context)
175   throws IOException, InterruptedException {
176     return new TableOutputCommitter();
177   }
178 
179   public Configuration getConf() {
180     return conf;
181   }
182 
183   @Override
184   public void setConf(Configuration otherConf) {
185     this.conf = HBaseConfiguration.create(otherConf);
186 
187     String tableName = this.conf.get(OUTPUT_TABLE);
188     if(tableName == null || tableName.length() <= 0) {
189       throw new IllegalArgumentException("Must specify table name");
190     }
191 
192     String address = this.conf.get(QUORUM_ADDRESS);
193     int zkClientPort = this.conf.getInt(QUORUM_PORT, 0);
194     String serverClass = this.conf.get(REGION_SERVER_CLASS);
195     String serverImpl = this.conf.get(REGION_SERVER_IMPL);
196 
197     try {
198       if (address != null) {
199         ZKUtil.applyClusterKeyToConf(this.conf, address);
200       }
201       if (serverClass != null) {
202         this.conf.set(HConstants.REGION_SERVER_IMPL, serverImpl);
203       }
204       if (zkClientPort != 0) {
205         this.conf.setInt(HConstants.ZOOKEEPER_CLIENT_PORT, zkClientPort);
206       }
207       this.table = new HTable(this.conf, tableName);
208       this.table.setAutoFlush(false, true);
209       LOG.info("Created table instance for "  + tableName);
210     } catch(IOException e) {
211       LOG.error(e);
212       throw new RuntimeException(e);
213     }
214   }
215 }