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.mapred.join; |
---|
20 | |
---|
21 | import java.io.DataInput; |
---|
22 | import java.io.DataOutput; |
---|
23 | import java.io.IOException; |
---|
24 | import java.util.HashSet; |
---|
25 | |
---|
26 | import org.apache.hadoop.io.Text; |
---|
27 | import org.apache.hadoop.io.WritableUtils; |
---|
28 | import org.apache.hadoop.mapred.InputSplit; |
---|
29 | import org.apache.hadoop.util.ReflectionUtils; |
---|
30 | |
---|
31 | /** |
---|
32 | * This InputSplit contains a set of child InputSplits. Any InputSplit inserted |
---|
33 | * into this collection must have a public default constructor. |
---|
34 | */ |
---|
35 | public class CompositeInputSplit implements InputSplit { |
---|
36 | |
---|
37 | private int fill = 0; |
---|
38 | private long totsize = 0L; |
---|
39 | private InputSplit[] splits; |
---|
40 | |
---|
41 | public CompositeInputSplit() { } |
---|
42 | |
---|
43 | public CompositeInputSplit(int capacity) { |
---|
44 | splits = new InputSplit[capacity]; |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Add an InputSplit to this collection. |
---|
49 | * @throws IOException If capacity was not specified during construction |
---|
50 | * or if capacity has been reached. |
---|
51 | */ |
---|
52 | public void add(InputSplit s) throws IOException { |
---|
53 | if (null == splits) { |
---|
54 | throw new IOException("Uninitialized InputSplit"); |
---|
55 | } |
---|
56 | if (fill == splits.length) { |
---|
57 | throw new IOException("Too many splits"); |
---|
58 | } |
---|
59 | splits[fill++] = s; |
---|
60 | totsize += s.getLength(); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Get ith child InputSplit. |
---|
65 | */ |
---|
66 | public InputSplit get(int i) { |
---|
67 | return splits[i]; |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * Return the aggregate length of all child InputSplits currently added. |
---|
72 | */ |
---|
73 | public long getLength() throws IOException { |
---|
74 | return totsize; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Get the length of ith child InputSplit. |
---|
79 | */ |
---|
80 | public long getLength(int i) throws IOException { |
---|
81 | return splits[i].getLength(); |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Collect a set of hosts from all child InputSplits. |
---|
86 | */ |
---|
87 | public String[] getLocations() throws IOException { |
---|
88 | HashSet<String> hosts = new HashSet<String>(); |
---|
89 | for (InputSplit s : splits) { |
---|
90 | String[] hints = s.getLocations(); |
---|
91 | if (hints != null && hints.length > 0) { |
---|
92 | for (String host : hints) { |
---|
93 | hosts.add(host); |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | return hosts.toArray(new String[hosts.size()]); |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * getLocations from ith InputSplit. |
---|
102 | */ |
---|
103 | public String[] getLocation(int i) throws IOException { |
---|
104 | return splits[i].getLocations(); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Write splits in the following format. |
---|
109 | * {@code |
---|
110 | * <count><class1><class2>...<classn><split1><split2>...<splitn> |
---|
111 | * } |
---|
112 | */ |
---|
113 | public void write(DataOutput out) throws IOException { |
---|
114 | WritableUtils.writeVInt(out, splits.length); |
---|
115 | for (InputSplit s : splits) { |
---|
116 | Text.writeString(out, s.getClass().getName()); |
---|
117 | } |
---|
118 | for (InputSplit s : splits) { |
---|
119 | s.write(out); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * {@inheritDoc} |
---|
125 | * @throws IOException If the child InputSplit cannot be read, typically |
---|
126 | * for faliing access checks. |
---|
127 | */ |
---|
128 | @SuppressWarnings("unchecked") // Generic array assignment |
---|
129 | public void readFields(DataInput in) throws IOException { |
---|
130 | int card = WritableUtils.readVInt(in); |
---|
131 | if (splits == null || splits.length != card) { |
---|
132 | splits = new InputSplit[card]; |
---|
133 | } |
---|
134 | Class<? extends InputSplit>[] cls = new Class[card]; |
---|
135 | try { |
---|
136 | for (int i = 0; i < card; ++i) { |
---|
137 | cls[i] = |
---|
138 | Class.forName(Text.readString(in)).asSubclass(InputSplit.class); |
---|
139 | } |
---|
140 | for (int i = 0; i < card; ++i) { |
---|
141 | splits[i] = ReflectionUtils.newInstance(cls[i], null); |
---|
142 | splits[i].readFields(in); |
---|
143 | } |
---|
144 | } catch (ClassNotFoundException e) { |
---|
145 | throw (IOException)new IOException("Failed split init").initCause(e); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | } |
---|