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