forked from twitter/elephant-bird
-
Notifications
You must be signed in to change notification settings - Fork 7
/
DeprecatedInputFormatWrapper.java
429 lines (359 loc) · 13.7 KB
/
DeprecatedInputFormatWrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package com.twitter.elephantbird.mapred.input;
import java.io.*;
import java.util.List;
import com.twitter.elephantbird.util.HadoopCompat;
import com.twitter.elephantbird.util.SplitUtil;
import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.serializer.Deserializer;
import org.apache.hadoop.io.serializer.SerializationFactory;
import org.apache.hadoop.io.serializer.Serializer;
import org.apache.hadoop.mapred.Counters;
import org.apache.hadoop.mapred.FileSplit;
import org.apache.hadoop.mapred.InputSplit;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.RecordReader;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapreduce.InputFormat;
import org.apache.hadoop.mapreduce.StatusReporter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.TaskAttemptID;
import org.apache.hadoop.util.ReflectionUtils;
import com.twitter.elephantbird.mapreduce.input.MapredInputFormatCompatible;
import com.twitter.elephantbird.mapred.output.DeprecatedOutputFormatWrapper;
import com.twitter.elephantbird.util.HadoopUtils;
/**
* The wrapper enables an {@link InputFormat} written for new
* <code>mapreduce</code> interface to be used unmodified in contexts where
* a {@link org.apache.hadoop.mapred.InputFormat} with old <code>mapred</code>
* interface is required. </p>
*
* Current restrictions on InputFormat: <ul>
* <li> the record reader should reuse key and value objects
* or implement {@link com.twitter.elephantbird.mapred.input.MapredInputFormatCompatible} </li>
* </ul>
*
* While this restriction is satisfied by most input formats,
* it could be removed with a configuration option.
* <p>
*
* Usage: <pre>
* // set InputFormat class using a mapreduce InputFormat
* DeprecatedInputFormatWrapper.setInputFormat(org.apache.hadoop.mapreduce.lib.input.TextInputFormat.class, jobConf);
* jobConf.setOutputFormat(org.apache.hadoop.mapred.TextOutputFormat.class);
* // ...
* </pre>
*
* @see DeprecatedOutputFormatWrapper
*
* @author Raghu Angadi
*
*/
@SuppressWarnings("deprecation")
public class DeprecatedInputFormatWrapper<K, V> implements org.apache.hadoop.mapred.InputFormat<K, V> {
public static final String CLASS_CONF_KEY = "elephantbird.class.for.DeprecatedInputFormatWrapper";
public static final String VALUE_COPY_CONF_KEY = "elephantbird.class.for.ValueCopyClass";
protected InputFormat<K, V> realInputFormat;
protected DeprecatedInputFormatValueCopier<V> valueCopier = null;
/**
* Sets jobs input format to {@link DeprecatedInputFormatWrapper} and stores
* supplied real {@link InputFormat} class name in job configuration.
* This configuration is read on the remote tasks to instantiate actual
* InputFormat correctly.
*/
public static void setInputFormat(Class<?> realInputFormatClass, JobConf jobConf) {
jobConf.setInputFormat(DeprecatedInputFormatWrapper.class);
HadoopUtils.setClassConf(jobConf, CLASS_CONF_KEY, realInputFormatClass);
}
/**
* For cases where we need to set hadoop1 input format in a hadoop2 Configuration object.
*/
public static void setInputFormat(Class<?> realInputFormatClass, Configuration conf) {
conf.setClass("mapred.input.format.class", DeprecatedInputFormatWrapper.class, org.apache.hadoop.mapred.InputFormat.class);
HadoopUtils.setClassConf(conf, CLASS_CONF_KEY, realInputFormatClass);
}
public static void setInputFormat(Class<?> realInputFormatClass, JobConf jobConf,
Class<? extends DeprecatedInputFormatValueCopier<?>> valueCopyClass) {
jobConf.setInputFormat(DeprecatedInputFormatWrapper.class);
HadoopUtils.setClassConf(jobConf, CLASS_CONF_KEY, realInputFormatClass);
HadoopUtils.setClassConf(jobConf, VALUE_COPY_CONF_KEY, valueCopyClass);
}
/**
* Only used in very specific cases and does not absolve one from
* calling the static setInptuFormat methods to set up the hadoop job
* properly.
* @param inputFormat
*/
public void setInputFormatInstance(InputFormat<K, V> inputFormat) {
realInputFormat = inputFormat;
}
@SuppressWarnings("unchecked")
private void initInputFormat(JobConf conf) {
if (realInputFormat == null) {
realInputFormat = ReflectionUtils.newInstance(conf.getClass(CLASS_CONF_KEY, null, InputFormat.class), conf);
if (conf.get(VALUE_COPY_CONF_KEY) != null) {
Class<? extends DeprecatedInputFormatValueCopier> copierClass = conf.getClass(VALUE_COPY_CONF_KEY,
null, DeprecatedInputFormatValueCopier.class);
if (null != copierClass) {
valueCopier = ReflectionUtils.newInstance(copierClass, conf);
}
}
}
}
public DeprecatedInputFormatWrapper() {
// real inputFormat is initialized based on conf.
}
public DeprecatedInputFormatWrapper(InputFormat<K, V> realInputFormat) {
this.realInputFormat = realInputFormat;
}
@Override
public RecordReader<K, V> getRecordReader(InputSplit split, JobConf job,
Reporter reporter) throws IOException {
initInputFormat(job);
return new RecordReaderWrapper<K, V>(realInputFormat, split, job, reporter, valueCopier);
}
@Override
public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException {
initInputFormat(job);
try {
List<org.apache.hadoop.mapreduce.InputSplit> splits =
realInputFormat.getSplits(HadoopCompat.newJobContext(job, null));
if (splits == null) {
return null;
}
InputSplit[] resultSplits = new InputSplit[splits.size()];
int i = 0;
for (org.apache.hadoop.mapreduce.InputSplit split : splits) {
if (split.getClass() == org.apache.hadoop.mapreduce.lib.input.FileSplit.class) {
org.apache.hadoop.mapreduce.lib.input.FileSplit mapreduceFileSplit =
((org.apache.hadoop.mapreduce.lib.input.FileSplit)split);
resultSplits[i++] = new FileSplit(
mapreduceFileSplit.getPath(),
mapreduceFileSplit.getStart(),
mapreduceFileSplit.getLength(),
mapreduceFileSplit.getLocations());
} else {
InputSplitWrapper wrapper = new InputSplitWrapper(split);
wrapper.setConf(job);
resultSplits[i++] = wrapper;
}
}
return resultSplits;
} catch (InterruptedException e) {
throw new IOException(e);
}
}
/**
* A reporter that works with both mapred and mapreduce APIs.
*/
public static class ReporterWrapper extends StatusReporter implements Reporter {
private Reporter wrappedReporter;
public ReporterWrapper(Reporter reporter) {
wrappedReporter = reporter;
}
@Override
public Counters.Counter getCounter(Enum<?> anEnum) {
return wrappedReporter.getCounter(anEnum);
}
@Override
public Counters.Counter getCounter(String s, String s1) {
return wrappedReporter.getCounter(s, s1);
}
@Override
public void incrCounter(Enum<?> anEnum, long l) {
wrappedReporter.incrCounter(anEnum, l);
}
@Override
public void incrCounter(String s, String s1, long l) {
wrappedReporter.incrCounter(s, s1, l);
}
@Override
public InputSplit getInputSplit() throws UnsupportedOperationException {
return wrappedReporter.getInputSplit();
}
@Override
public void progress() {
wrappedReporter.progress();
}
// @Override
public float getProgress() {
throw new UnsupportedOperationException();
}
@Override
public void setStatus(String s) {
wrappedReporter.setStatus(s);
}
}
private static class RecordReaderWrapper<K, V> implements RecordReader<K, V> {
private org.apache.hadoop.mapreduce.RecordReader<K, V> realReader;
private MapredInputFormatCompatible mifcReader = null;
private long splitLen; // for getPos()
// expect readReader return same Key & Value objects (common case)
// this avoids extra serialization & deserialazion of these objects
private K keyObj = null;
private V valueObj = null;
private boolean firstRecord = false;
private boolean eof = false;
private DeprecatedInputFormatValueCopier<V> valueCopier = null;
public RecordReaderWrapper(InputFormat<K, V> newInputFormat, InputSplit oldSplit, JobConf oldJobConf,
Reporter reporter, DeprecatedInputFormatValueCopier<V> valueCopier) throws IOException {
this.valueCopier = valueCopier;
splitLen = oldSplit.getLength();
org.apache.hadoop.mapreduce.InputSplit split;
if (oldSplit.getClass() == FileSplit.class) {
split = new org.apache.hadoop.mapreduce.lib.input.FileSplit(
((FileSplit)oldSplit).getPath(),
((FileSplit)oldSplit).getStart(),
((FileSplit)oldSplit).getLength(),
oldSplit.getLocations());
} else {
split = ((InputSplitWrapper)oldSplit).realSplit;
}
TaskAttemptID taskAttemptID = TaskAttemptID.forName(oldJobConf.get("mapred.task.id"));
if (taskAttemptID == null) {
taskAttemptID = new TaskAttemptID();
}
// create a MapContext to pass reporter to record reader (for counters)
TaskAttemptContext taskContext = HadoopCompat
.newMapContext(oldJobConf, taskAttemptID, null, null, null,
new ReporterWrapper(reporter), null);
try {
realReader = newInputFormat.createRecordReader(split, taskContext);
realReader.initialize(split, taskContext);
if (realReader instanceof MapredInputFormatCompatible) {
mifcReader = ((MapredInputFormatCompatible) realReader);
}
} catch (InterruptedException e) {
throw new IOException(e);
}
}
private void initKeyValueObjects() {
// read once to gain access to key and value objects
try {
if (!firstRecord & !eof) {
if (realReader.nextKeyValue()) {
firstRecord = true;
keyObj = realReader.getCurrentKey();
valueObj = realReader.getCurrentValue();
} else {
eof = true;
}
}
} catch (Exception e) {
throw new RuntimeException("Could not read first record (and it was not an EOF)", e);
}
}
@Override
public void close() throws IOException {
realReader.close();
}
@Override
public K createKey() {
initKeyValueObjects();
return keyObj;
}
@Override
public V createValue() {
initKeyValueObjects();
return valueObj;
}
@Override
public long getPos() throws IOException {
return (long) (splitLen * getProgress());
}
@Override
public float getProgress() throws IOException {
try {
return realReader.getProgress();
} catch (InterruptedException e) {
throw new IOException(e);
}
}
@Override
public boolean next(K key, V value) throws IOException {
if (eof) {
return false;
}
if (firstRecord) { // key & value are already read.
firstRecord = false;
return true;
}
if (mifcReader != null) {
mifcReader.setKeyValue(key, value);
}
try {
if (realReader.nextKeyValue()) {
if (key != realReader.getCurrentKey()) {
if (mifcReader != null) {
throw new IOException("The RecordReader returned a key and value that do not match "
+ "the key and value sent to it. This means the RecordReader did not properly implement "
+ "com.twitter.elephantbird.mapred.input.MapredInputFormatCompatible. "
+ "Current reader class : " + realReader.getClass());
} else {
throw new IOException("DeprecatedInputFormatWrapper only "
+ "supports RecordReaders that return the same key & value "
+ "objects or implement com.twitter.elephantbird.mapred.input.MapredInputFormatCompatible. "
+ "Current reader class : " + realReader.getClass());
}
}
if (value != realReader.getCurrentValue()) {
if (null != valueCopier)
valueCopier.copyValue(value, realReader.getCurrentValue());
else {
throw new IOException("DeprecatedInputFormatWrapper - value is different "
+ "and no value copier provided. "
+ "Current reader class : " + realReader.getClass());
}
}
return true;
}
} catch (InterruptedException e) {
throw new IOException(e);
}
eof = true; // strictly not required, just for consistency
return false;
}
}
private static class InputSplitWrapper implements InputSplit, Configurable {
org.apache.hadoop.mapreduce.InputSplit realSplit;
private Configuration conf;
@SuppressWarnings("unused") // MapReduce instantiates this.
public InputSplitWrapper() {}
public InputSplitWrapper(org.apache.hadoop.mapreduce.InputSplit realSplit) {
this.realSplit = realSplit;
}
@Override
public long getLength() throws IOException {
try {
return realSplit.getLength();
} catch (InterruptedException e) {
throw new IOException(e);
}
}
@Override
public String[] getLocations() throws IOException {
try {
return realSplit.getLocations();
} catch (InterruptedException e) {
throw new IOException(e);
}
}
@Override
public void readFields(DataInput in) throws IOException {
realSplit = SplitUtil.deserializeInputSplit(conf, (DataInputStream) in);
}
@Override
public void write(DataOutput out) throws IOException {
SplitUtil.serializeInputSplit(conf, (DataOutputStream) out, realSplit);
}
@Override
public void setConf(Configuration conf) {
this.conf = conf;
}
@Override
public Configuration getConf() {
return conf;
}
}
}