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 java.io.IOException;
022    import java.io.DataInput;
023    import java.io.DataOutput;
024    
025    import org.apache.hadoop.mapreduce.InputFormat;
026    import org.apache.hadoop.mapreduce.InputSplit;
027    import org.apache.hadoop.mapreduce.TaskAttemptContext;
028    import org.apache.hadoop.classification.InterfaceAudience;
029    import org.apache.hadoop.classification.InterfaceStability;
030    import org.apache.hadoop.fs.Path;
031    import org.apache.hadoop.io.Text;
032    import org.apache.hadoop.io.Writable;
033    
034    /** A section of an input file.  Returned by {@link
035     * InputFormat#getSplits(JobContext)} and passed to
036     * {@link InputFormat#createRecordReader(InputSplit,TaskAttemptContext)}. */
037    @InterfaceAudience.Public
038    @InterfaceStability.Stable
039    public class FileSplit extends InputSplit implements Writable {
040      private Path file;
041      private long start;
042      private long length;
043      private String[] hosts;
044    
045      public FileSplit() {}
046    
047      /** Constructs a split with host information
048       *
049       * @param file the file name
050       * @param start the position of the first byte in the file to process
051       * @param length the number of bytes in the file to process
052       * @param hosts the list of hosts containing the block, possibly null
053       */
054      public FileSplit(Path file, long start, long length, String[] hosts) {
055        this.file = file;
056        this.start = start;
057        this.length = length;
058        this.hosts = hosts;
059      }
060     
061      /** The file containing this split's data. */
062      public Path getPath() { return file; }
063      
064      /** The position of the first byte in the file to process. */
065      public long getStart() { return start; }
066      
067      /** The number of bytes in the file to process. */
068      @Override
069      public long getLength() { return length; }
070    
071      @Override
072      public String toString() { return file + ":" + start + "+" + length; }
073    
074      ////////////////////////////////////////////
075      // Writable methods
076      ////////////////////////////////////////////
077    
078      @Override
079      public void write(DataOutput out) throws IOException {
080        Text.writeString(out, file.toString());
081        out.writeLong(start);
082        out.writeLong(length);
083      }
084    
085      @Override
086      public void readFields(DataInput in) throws IOException {
087        file = new Path(Text.readString(in));
088        start = in.readLong();
089        length = in.readLong();
090        hosts = null;
091      }
092    
093      @Override
094      public String[] getLocations() throws IOException {
095        if (this.hosts == null) {
096          return new String[]{};
097        } else {
098          return this.hosts;
099        }
100      }
101    }