1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.mapreduce;
20
21 import java.io.IOException;
22 import java.lang.reflect.Constructor;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.hbase.classification.InterfaceAudience;
29 import org.apache.hadoop.hbase.classification.InterfaceStability;
30 import org.apache.hadoop.fs.Path;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.mapred.JobClient;
33 import org.apache.hadoop.mapred.JobConf;
34 import org.apache.hadoop.mapreduce.JobSubmissionFiles;
35
36
37
38
39 @InterfaceAudience.Private
40 @InterfaceStability.Evolving
41 public abstract class JobUtil {
42 private static final Log LOG = LogFactory.getLog(JobUtil.class);
43
44 protected JobUtil() {
45 super();
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public static Path getStagingDir(Configuration conf)
63 throws IOException, InterruptedException {
64 Path stagingDirPath = null;
65
66
67 try {
68 stagingDirPath = getStagingDirFromMR1(conf);
69 } catch (NoSuchMethodException e) {
70 stagingDirPath = getStagingDirFromMR2(conf);
71 }
72 if (stagingDirPath != null) LOG.debug("Staging dir of the job is: " + stagingDirPath);
73 return stagingDirPath;
74 }
75
76
77
78
79
80
81
82
83
84 private static Path getStagingDirFromMR1(Configuration conf) throws IOException,
85 NoSuchMethodException, InterruptedException {
86 Path stagingDirPath;
87 JobClient jobClient = new JobClient(new JobConf(conf));
88 Method getStagingDirMethod = JobSubmissionFiles.class.getMethod("getStagingDir",
89 jobClient.getClass(), conf.getClass());
90 try {
91
92
93 stagingDirPath = (Path) getStagingDirMethod.invoke(null, jobClient, conf);
94 } catch (IllegalArgumentException iae) {
95 throw new IllegalStateException(iae);
96 } catch (IllegalAccessException e) {
97 throw new IllegalStateException(e);
98 } catch (InvocationTargetException ite) {
99 throw new IllegalStateException(ite);
100 }
101 return stagingDirPath;
102 }
103
104
105
106
107
108
109
110 private static Path getStagingDirFromMR2(Configuration conf) {
111 Path stagingDirPath = null;
112 try {
113 Class<?> clusterClass = Class.forName("org.apache.hadoop.mapreduce.Cluster");
114 Method getStagingDirMethod = JobSubmissionFiles.class.getMethod("getStagingDir",
115 clusterClass, conf.getClass());
116 Constructor<?> ctr = clusterClass.getConstructor(conf.getClass());
117 Object clusterInstance = ctr.newInstance(conf);
118
119
120 stagingDirPath = (Path) getStagingDirMethod.invoke(null, clusterInstance, conf);
121 } catch (ClassNotFoundException cnfe) {
122 throw new IllegalStateException(cnfe);
123 } catch (SecurityException se) {
124 throw new IllegalStateException(se);
125 } catch (NoSuchMethodException nsme) {
126 throw new IllegalStateException(nsme);
127 } catch (IllegalArgumentException iae) {
128 throw new IllegalStateException(iae);
129 } catch (InstantiationException ie) {
130 throw new IllegalStateException(ie);
131 } catch (IllegalAccessException e) {
132 throw new IllegalStateException(e);
133 } catch (InvocationTargetException ite) {
134 throw new IllegalStateException(ite);
135 }
136 return stagingDirPath;
137 }
138 }