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