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