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.mapreduce.lib.db;
020
021 import java.io.DataInput;
022 import java.io.DataOutput;
023 import java.io.IOException;
024 import java.sql.Connection;
025 import java.sql.DatabaseMetaData;
026 import java.sql.PreparedStatement;
027 import java.sql.ResultSet;
028 import java.sql.SQLException;
029 import java.sql.Statement;
030 import java.sql.Types;
031 import java.util.ArrayList;
032 import java.util.List;
033
034 import org.apache.commons.logging.Log;
035 import org.apache.commons.logging.LogFactory;
036
037 import org.apache.hadoop.io.LongWritable;
038 import org.apache.hadoop.io.Text;
039 import org.apache.hadoop.io.Writable;
040 import org.apache.hadoop.mapreduce.InputFormat;
041 import org.apache.hadoop.mapreduce.InputSplit;
042 import org.apache.hadoop.mapreduce.Job;
043 import org.apache.hadoop.mapreduce.JobContext;
044 import org.apache.hadoop.mapreduce.RecordReader;
045 import org.apache.hadoop.mapreduce.TaskAttemptContext;
046 import org.apache.hadoop.util.ReflectionUtils;
047 import org.apache.hadoop.classification.InterfaceAudience;
048 import org.apache.hadoop.classification.InterfaceStability;
049 import org.apache.hadoop.conf.Configurable;
050 import org.apache.hadoop.conf.Configuration;
051
052 /**
053 * A InputFormat that reads input data from an SQL table in an Oracle db.
054 */
055 @InterfaceAudience.Public
056 @InterfaceStability.Evolving
057 public class OracleDataDrivenDBInputFormat<T extends DBWritable>
058 extends DataDrivenDBInputFormat<T> implements Configurable {
059
060 /**
061 * @return the DBSplitter implementation to use to divide the table/query into InputSplits.
062 */
063 @Override
064 protected DBSplitter getSplitter(int sqlDataType) {
065 switch (sqlDataType) {
066 case Types.DATE:
067 case Types.TIME:
068 case Types.TIMESTAMP:
069 return new OracleDateSplitter();
070
071 default:
072 return super.getSplitter(sqlDataType);
073 }
074 }
075
076 @Override
077 protected RecordReader<LongWritable, T> createDBRecordReader(DBInputSplit split,
078 Configuration conf) throws IOException {
079
080 DBConfiguration dbConf = getDBConf();
081 @SuppressWarnings("unchecked")
082 Class<T> inputClass = (Class<T>) (dbConf.getInputClass());
083
084 try {
085 // Use Oracle-specific db reader
086 return new OracleDataDrivenDBRecordReader<T>(split, inputClass,
087 conf, getConnection(), dbConf, dbConf.getInputConditions(),
088 dbConf.getInputFieldNames(), dbConf.getInputTableName());
089 } catch (SQLException ex) {
090 throw new IOException(ex.getMessage());
091 }
092 }
093 }