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 019package org.apache.hadoop.mapreduce.lib.db; 020 021import java.io.DataInput; 022import java.io.DataOutput; 023import java.io.IOException; 024import java.sql.Connection; 025import java.sql.DatabaseMetaData; 026import java.sql.PreparedStatement; 027import java.sql.ResultSet; 028import java.sql.SQLException; 029import java.sql.Statement; 030import java.sql.Types; 031import java.util.ArrayList; 032import java.util.List; 033 034import org.apache.commons.logging.Log; 035import org.apache.commons.logging.LogFactory; 036 037import org.apache.hadoop.io.LongWritable; 038import org.apache.hadoop.io.Text; 039import org.apache.hadoop.io.Writable; 040import org.apache.hadoop.mapreduce.InputFormat; 041import org.apache.hadoop.mapreduce.InputSplit; 042import org.apache.hadoop.mapreduce.Job; 043import org.apache.hadoop.mapreduce.JobContext; 044import org.apache.hadoop.mapreduce.RecordReader; 045import org.apache.hadoop.mapreduce.TaskAttemptContext; 046import org.apache.hadoop.util.ReflectionUtils; 047import org.apache.hadoop.classification.InterfaceAudience; 048import org.apache.hadoop.classification.InterfaceStability; 049import org.apache.hadoop.conf.Configurable; 050import 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 057public 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, createConnection(), dbConf, dbConf.getInputConditions(), 088 dbConf.getInputFieldNames(), dbConf.getInputTableName()); 089 } catch (SQLException ex) { 090 throw new IOException(ex.getMessage()); 091 } 092 } 093}