1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | |
---|
19 | package org.apache.hadoop.mapreduce.lib.input; |
---|
20 | |
---|
21 | import java.io.IOException; |
---|
22 | import java.io.DataInput; |
---|
23 | import java.io.DataOutput; |
---|
24 | |
---|
25 | import org.apache.hadoop.mapreduce.InputFormat; |
---|
26 | import org.apache.hadoop.mapreduce.InputSplit; |
---|
27 | import org.apache.hadoop.mapreduce.TaskAttemptContext; |
---|
28 | import org.apache.hadoop.fs.Path; |
---|
29 | import org.apache.hadoop.io.Text; |
---|
30 | import org.apache.hadoop.io.Writable; |
---|
31 | |
---|
32 | /** A section of an input file. Returned by {@link |
---|
33 | * InputFormat#getSplits(JobContext)} and passed to |
---|
34 | * {@link InputFormat#createRecordReader(InputSplit,TaskAttemptContext)}. */ |
---|
35 | public class FileSplit extends InputSplit implements Writable { |
---|
36 | private Path file; |
---|
37 | private long start; |
---|
38 | private long length; |
---|
39 | private String[] hosts; |
---|
40 | |
---|
41 | FileSplit() {} |
---|
42 | |
---|
43 | /** Constructs a split with host information |
---|
44 | * |
---|
45 | * @param file the file name |
---|
46 | * @param start the position of the first byte in the file to process |
---|
47 | * @param length the number of bytes in the file to process |
---|
48 | * @param hosts the list of hosts containing the block, possibly null |
---|
49 | */ |
---|
50 | public FileSplit(Path file, long start, long length, String[] hosts) { |
---|
51 | this.file = file; |
---|
52 | this.start = start; |
---|
53 | this.length = length; |
---|
54 | this.hosts = hosts; |
---|
55 | } |
---|
56 | |
---|
57 | /** The file containing this split's data. */ |
---|
58 | public Path getPath() { return file; } |
---|
59 | |
---|
60 | /** The position of the first byte in the file to process. */ |
---|
61 | public long getStart() { return start; } |
---|
62 | |
---|
63 | /** The number of bytes in the file to process. */ |
---|
64 | @Override |
---|
65 | public long getLength() { return length; } |
---|
66 | |
---|
67 | @Override |
---|
68 | public String toString() { return file + ":" + start + "+" + length; } |
---|
69 | |
---|
70 | //////////////////////////////////////////// |
---|
71 | // Writable methods |
---|
72 | //////////////////////////////////////////// |
---|
73 | |
---|
74 | @Override |
---|
75 | public void write(DataOutput out) throws IOException { |
---|
76 | Text.writeString(out, file.toString()); |
---|
77 | out.writeLong(start); |
---|
78 | out.writeLong(length); |
---|
79 | } |
---|
80 | |
---|
81 | @Override |
---|
82 | public void readFields(DataInput in) throws IOException { |
---|
83 | file = new Path(Text.readString(in)); |
---|
84 | start = in.readLong(); |
---|
85 | length = in.readLong(); |
---|
86 | hosts = null; |
---|
87 | } |
---|
88 | |
---|
89 | @Override |
---|
90 | public String[] getLocations() throws IOException { |
---|
91 | if (this.hosts == null) { |
---|
92 | return new String[]{}; |
---|
93 | } else { |
---|
94 | return this.hosts; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|