1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.mapreduce;
19
20 import java.io.IOException;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.Method;
23
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.mapreduce.Job;
26 import org.apache.hadoop.mapreduce.JobContext;
27 import org.apache.hadoop.mapreduce.JobID;
28
29
30
31
32
33
34
35 abstract public class MapreduceTestingShim {
36 private static MapreduceTestingShim instance;
37
38 static {
39 try {
40
41 Class c = Class
42 .forName("org.apache.hadoop.mapreduce.task.TaskAttemptContextImpl");
43 instance = new MapreduceV2Shim();
44 } catch (Exception e) {
45 instance = new MapreduceV1Shim();
46 }
47 }
48
49 abstract public JobContext newJobContext(Configuration jobConf)
50 throws IOException;
51
52 public static JobContext createJobContext(Configuration jobConf)
53 throws IOException {
54 return instance.newJobContext(jobConf);
55 }
56
57 private static class MapreduceV1Shim extends MapreduceTestingShim {
58 public JobContext newJobContext(Configuration jobConf) throws IOException {
59
60
61 JobID jobId = new JobID();
62 Constructor<JobContext> c;
63 try {
64 c = JobContext.class.getConstructor(Configuration.class, JobID.class);
65 return c.newInstance(jobConf, jobId);
66 } catch (Exception e) {
67 throw new IllegalStateException(
68 "Failed to instantiate new JobContext(jobConf, new JobID())", e);
69 }
70 }
71 };
72
73 private static class MapreduceV2Shim extends MapreduceTestingShim {
74 public JobContext newJobContext(Configuration jobConf) {
75
76
77 try {
78 Method m = Job.class.getMethod("getInstance", Configuration.class);
79 return (JobContext) m.invoke(null, jobConf);
80 } catch (Exception e) {
81 e.printStackTrace();
82 throw new IllegalStateException(
83 "Failed to return from Job.getInstance(jobConf)");
84 }
85 }
86 };
87
88 }