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.mapred;
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.fs.FileSystem;
28  import org.apache.hadoop.hbase.HBaseConfiguration;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.client.HTable;
31  import org.apache.hadoop.hbase.client.Put;
32  import org.apache.hadoop.hbase.client.Table;
33  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
34  import org.apache.hadoop.fs.FileAlreadyExistsException;
35  import org.apache.hadoop.mapred.InvalidJobConfException;
36  import org.apache.hadoop.mapred.JobConf;
37  import org.apache.hadoop.mapred.FileOutputFormat;
38  import org.apache.hadoop.mapred.RecordWriter;
39  import org.apache.hadoop.mapred.Reporter;
40  import org.apache.hadoop.util.Progressable;
41  
42  /**
43   * Convert Map/Reduce output and write it to an HBase table
44   */
45  @InterfaceAudience.Public
46  @InterfaceStability.Stable
47  public class TableOutputFormat extends
48  FileOutputFormat<ImmutableBytesWritable, Put> {
49  
50    /** JobConf parameter that specifies the output table */
51    public static final String OUTPUT_TABLE = "hbase.mapred.outputtable";
52    private static final Log LOG = LogFactory.getLog(TableOutputFormat.class);
53  
54    /**
55     * Convert Reduce output (key, value) to (HStoreKey, KeyedDataArrayWritable)
56     * and write to an HBase table
57     */
58    protected static class TableRecordWriter
59      implements RecordWriter<ImmutableBytesWritable, Put> {
60      private Table m_table;
61  
62      /**
63       * Instantiate a TableRecordWriter with the HBase HClient for writing.
64       *
65       * @param table
66       */
67      public TableRecordWriter(Table table) {
68        m_table = table;
69      }
70  
71      public void close(Reporter reporter)
72        throws IOException {
73        m_table.close();
74      }
75  
76      public void write(ImmutableBytesWritable key,
77          Put value) throws IOException {
78        m_table.put(new Put(value));
79      }
80    }
81  
82    @Override
83    @SuppressWarnings("unchecked")
84    public RecordWriter getRecordWriter(FileSystem ignored,
85        JobConf job, String name, Progressable progress) throws IOException {
86  
87      // expecting exactly one path
88  
89      TableName tableName = TableName.valueOf(job.get(OUTPUT_TABLE));
90      HTable table = null;
91      try {
92        table = new HTable(HBaseConfiguration.create(job), tableName);
93      } catch(IOException e) {
94        LOG.error(e);
95        throw e;
96      }
97      table.setAutoFlush(false, true);
98      return new TableRecordWriter(table);
99    }
100 
101   @Override
102   public void checkOutputSpecs(FileSystem ignored, JobConf job)
103   throws FileAlreadyExistsException, InvalidJobConfException, IOException {
104 
105     String tableName = job.get(OUTPUT_TABLE);
106     if(tableName == null) {
107       throw new IOException("Must specify table name");
108     }
109   }
110 }