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;
020    
021    import java.io.*;
022    
023    import org.apache.hadoop.classification.InterfaceAudience;
024    import org.apache.hadoop.classification.InterfaceStability;
025    import org.apache.hadoop.fs.*;
026    import org.apache.hadoop.io.LongWritable;
027    import org.apache.hadoop.io.Text;
028    import org.apache.hadoop.io.compress.*;
029    
030    import com.google.common.base.Charsets;
031    
032    /** 
033     * An {@link InputFormat} for plain text files.  Files are broken into lines.
034     * Either linefeed or carriage-return are used to signal end of line.  Keys are
035     * the position in the file, and values are the line of text.. 
036     */
037    @InterfaceAudience.Public
038    @InterfaceStability.Stable
039    public class TextInputFormat extends FileInputFormat<LongWritable, Text>
040      implements JobConfigurable {
041    
042      private CompressionCodecFactory compressionCodecs = null;
043      
044      public void configure(JobConf conf) {
045        compressionCodecs = new CompressionCodecFactory(conf);
046      }
047      
048      protected boolean isSplitable(FileSystem fs, Path file) {
049        final CompressionCodec codec = compressionCodecs.getCodec(file);
050        if (null == codec) {
051          return true;
052        }
053        return codec instanceof SplittableCompressionCodec;
054      }
055    
056      public RecordReader<LongWritable, Text> getRecordReader(
057                                              InputSplit genericSplit, JobConf job,
058                                              Reporter reporter)
059        throws IOException {
060        
061        reporter.setStatus(genericSplit.toString());
062        String delimiter = job.get("textinputformat.record.delimiter");
063        byte[] recordDelimiterBytes = null;
064        if (null != delimiter) {
065          recordDelimiterBytes = delimiter.getBytes(Charsets.UTF_8);
066        }
067        return new LineRecordReader(job, (FileSplit) genericSplit,
068            recordDelimiterBytes);
069      }
070    }