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
035import 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
042public 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}