001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.hadoop.mapred.lib.db;
020    
021    import java.io.IOException;
022    import java.sql.Connection;
023    import java.sql.PreparedStatement;
024    import java.sql.SQLException;
025    
026    import org.apache.hadoop.classification.InterfaceAudience;
027    import org.apache.hadoop.classification.InterfaceStability;
028    import org.apache.hadoop.fs.FileSystem;
029    import org.apache.hadoop.mapred.JobConf;
030    import org.apache.hadoop.mapred.OutputFormat;
031    import org.apache.hadoop.mapred.RecordWriter;
032    import org.apache.hadoop.mapred.Reporter;
033    import org.apache.hadoop.mapreduce.MRJobConfig;
034    import org.apache.hadoop.mapreduce.TaskAttemptContext;
035    import org.apache.hadoop.mapreduce.TaskAttemptID;
036    import org.apache.hadoop.mapreduce.task.TaskAttemptContextImpl;
037    import org.apache.hadoop.util.Progressable;
038    
039    /**
040     * @deprecated Use org.apache.hadoop.mapreduce.lib.db.DBOutputFormat instead
041     */
042    @Deprecated
043    @InterfaceAudience.Public
044    @InterfaceStability.Stable
045    public class DBOutputFormat<K  extends DBWritable, V> 
046        extends org.apache.hadoop.mapreduce.lib.db.DBOutputFormat<K, V>
047        implements OutputFormat<K, V> {
048    
049      /**
050       * A RecordWriter that writes the reduce output to a SQL table
051       */
052      protected class DBRecordWriter extends 
053          org.apache.hadoop.mapreduce.lib.db.DBOutputFormat<K, V>.DBRecordWriter
054          implements RecordWriter<K, V> {
055    
056        protected DBRecordWriter(Connection connection, 
057          PreparedStatement statement) throws SQLException {
058          super(connection, statement);
059        }
060    
061        /** {@inheritDoc} */
062        public void close(Reporter reporter) throws IOException {
063          super.close(null);
064        }
065      }
066    
067      /** {@inheritDoc} */
068      public void checkOutputSpecs(FileSystem filesystem, JobConf job)
069      throws IOException {
070      }
071    
072    
073      /** {@inheritDoc} */
074      public RecordWriter<K, V> getRecordWriter(FileSystem filesystem,
075          JobConf job, String name, Progressable progress) throws IOException {
076        org.apache.hadoop.mapreduce.RecordWriter<K, V> w = super.getRecordWriter(
077          new TaskAttemptContextImpl(job, 
078                TaskAttemptID.forName(job.get(MRJobConfig.TASK_ATTEMPT_ID))));
079        org.apache.hadoop.mapreduce.lib.db.DBOutputFormat.DBRecordWriter writer = 
080         (org.apache.hadoop.mapreduce.lib.db.DBOutputFormat.DBRecordWriter) w;
081        try {
082          return new DBRecordWriter(writer.getConnection(), writer.getStatement());
083        } catch(SQLException se) {
084          throw new IOException(se);
085        }
086      }
087    
088      /**
089       * Initializes the reduce-part of the job with the appropriate output settings
090       * 
091       * @param job The job
092       * @param tableName The table to insert data into
093       * @param fieldNames The field names in the table.
094       */
095      public static void setOutput(JobConf job, String tableName, String... fieldNames) {
096        if(fieldNames.length > 0 && fieldNames[0] != null) {
097          DBConfiguration dbConf = setOutput(job, tableName);
098          dbConf.setOutputFieldNames(fieldNames);
099        } else {
100          if(fieldNames.length > 0)
101            setOutput(job, tableName, fieldNames.length);
102          else 
103            throw new IllegalArgumentException("Field names must be greater than 0");
104        }
105      }
106      
107      /**
108       * Initializes the reduce-part of the job with the appropriate output settings
109       * 
110       * @param job The job
111       * @param tableName The table to insert data into
112       * @param fieldCount the number of fields in the table.
113       */
114      public static void setOutput(JobConf job, String tableName, int fieldCount) {
115        DBConfiguration dbConf = setOutput(job, tableName);
116        dbConf.setOutputFieldCount(fieldCount);
117      }
118      
119      private static DBConfiguration setOutput(JobConf job, String tableName) {
120        job.setOutputFormat(DBOutputFormat.class);
121        job.setReduceSpeculativeExecution(false);
122    
123        DBConfiguration dbConf = new DBConfiguration(job);
124        
125        dbConf.setOutputTableName(tableName);
126        return dbConf;
127      }
128      
129    }