forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStepFactory.java
More file actions
290 lines (268 loc) · 10.2 KB
/
StepFactory.java
File metadata and controls
290 lines (268 loc) · 10.2 KB
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
/*
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.services.elasticmapreduce.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.amazonaws.services.elasticmapreduce.model.HadoopJarStepConfig;
import com.amazonaws.util.StringUtils;
/**
* This class provides helper methods for creating common Elastic MapReduce step
* types. To use StepFactory, you should construct it with the appropriate
* bucket for your region. The official bucket format is
* "<region>.elasticmapreduce", so us-east-1 would use the bucket
* "us-east-1.elasticmapreduce".
* <p>
* Example usage, create an interactive Hive job flow with debugging enabled:
* <pre>
* AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
* AmazonElasticMapReduce emr = new AmazonElasticMapReduceClient(credentials);
*
* StepFactory stepFactory = new StepFactory();
*
* StepConfig enableDebugging = new StepConfig()
* .withName("Enable Debugging")
* .withActionOnFailure("TERMINATE_JOB_FLOW")
* .withHadoopJarStep(stepFactory.newEnableDebuggingStep());
*
* StepConfig installHive = new StepConfig()
* .withName("Install Hive")
* .withActionOnFailure("TERMINATE_JOB_FLOW")
* .withHadoopJarStep(stepFactory.newInstallHiveStep());
*
* RunJobFlowRequest request = new RunJobFlowRequest()
* .withName("Hive Interactive")
* .withSteps(enableDebugging, installHive)
* .withLogUri("s3://log-bucket/")
* .withInstances(new JobFlowInstancesConfig()
* .withEc2KeyName("keypair")
* .withHadoopVersion("0.20")
* .withInstanceCount(5)
* .withKeepJobFlowAliveWhenNoSteps(true)
* .withMasterInstanceType("m1.small")
* .withSlaveInstanceType("m1.small"));
*
* RunJobFlowResult result = emr.runJobFlow(request);
* </pre>
*/
public class StepFactory {
private final String bucket;
/**
* The available Hive versions. These are only available on Hadoop 0.20
* Hive_0_5 Hive 0.5
* Hive_0_7 Hive 0.7
* Hive_0_7_1 Hive 0.7.1
*/
public static enum HiveVersion {
Hive_0_5("0.5"),
Hive_0_7("0.7"),
Hive_0_7_1("0.7.1"),
Hive_Latest("latest");
private String stringVal;
HiveVersion(String str) {
stringVal = str;
}
@Override
public String toString() {
return stringVal;
}
}
/**
* Creates a new StepFactory using the default Elastic Map Reduce bucket
* (us-east-1.elasticmapreduce) for the default (us-east-1) region.
*/
public StepFactory() {
this("us-east-1.elasticmapreduce");
}
/**
* Creates a new StepFactory using the specified Amazon S3 bucket to load
* resources.
* <p>
* The official bucket format is "<region>.elasticmapreduce", so if
* you're using the us-east-1 region, you should use the bucket
* "us-east-1.elasticmapreduce".
*
* @param bucket
* The Amazon S3 bucket from which to load resources.
*/
public StepFactory(String bucket) {
this.bucket = bucket;
}
/**
* Runs a specified script on the master node of your cluster.
*
* @param script
* The script to run.
* @param args
* Arguments that get passed to the script.
* @return HadoopJarStepConfig that can be passed to your job flow.
*/
public HadoopJarStepConfig newScriptRunnerStep(String script, String... args) {
List<String> argsList = new ArrayList<String>();
argsList.add(script);
for (String arg : args) {
argsList.add(arg);
}
return new HadoopJarStepConfig()
.withJar("s3://" + bucket + "/libs/script-runner/script-runner.jar")
.withArgs(argsList);
}
/**
* When ran as the first step in your job flow, enables the Hadoop debugging
* UI in the AWS Management Console.
*
* @return HadoopJarStepConfig that can be passed to your job flow.
*/
public HadoopJarStepConfig newEnableDebuggingStep() {
return newScriptRunnerStep("s3://" + bucket + "/libs/state-pusher/0.1/fetch");
}
/**
* Step that installs the specified versions of Hive on your job flow.
*
* @param hiveVersions the versions of Hive to install
* @return HadoopJarStepConfig that can be passed to your job flow.
*/
public HadoopJarStepConfig newInstallHiveStep(HiveVersion... hiveVersions) {
if (hiveVersions.length > 0) {
String[] versionStrings = new String[hiveVersions.length];
for (int i = 0; i < hiveVersions.length; i++) {
versionStrings[i] = hiveVersions[i].toString();
}
return newInstallHiveStep(versionStrings);
}
return newHivePigStep("hive", "--install-hive", "--hive-versions", "latest");
}
/**
* Step that installs the specified versions of Hive on your job flow.
*
* @param hiveVersions the versions of Hive to install
* @return HadoopJarStepConfig that can be passed to your job flow.
*/
public HadoopJarStepConfig newInstallHiveStep(String... hiveVersions) {
if (hiveVersions.length > 0) {
return newHivePigStep("hive", "--install-hive", "--hive-versions",
StringUtils.join(",", hiveVersions));
}
return newHivePigStep("hive", "--install-hive", "--hive-versions", "latest");
}
/**
* Step that installs the default version of Hive on your job flow. This is
* 0.4 for Hadoop 0.18 and 0.5 for Hadoop 0.20.
*
* @return HadoopJarStepConfig that can be passed to your job flow.
*/
public HadoopJarStepConfig newInstallHiveStep() {
return newInstallHiveStep(new HiveVersion[0]);
}
/**
* Step that runs a Hive script on your job flow using the specified Hive version.
*
* @param script
* The script to run.
* @param hiveVersion
* The Hive version to use.
* @param scriptArgs
* Arguments that get passed to the script.
* @return HadoopJarStepConfig that can be passed to your job flow.
*/
public HadoopJarStepConfig newRunHiveScriptStepVersioned(String script,
String hiveVersion, String... scriptArgs) {
List<String> hiveArgs = new ArrayList<String>();
hiveArgs.add("--hive-versions");
hiveArgs.add(hiveVersion);
hiveArgs.add("--run-hive-script");
hiveArgs.add("--args");
hiveArgs.add("-f");
hiveArgs.add(script);
hiveArgs.addAll(Arrays.asList(scriptArgs));
return newHivePigStep("hive", hiveArgs.toArray(new String[0]));
}
/**
* Step that runs a Hive script on your job flow using the default Hive version.
*
* @param script
* The script to run.
* @param args
* Arguments that get passed to the script.
* @return HadoopJarStepConfig that can be passed to your job flow.
*/
public HadoopJarStepConfig newRunHiveScriptStep(String script, String... args) {
return newRunHiveScriptStepVersioned(script, "latest", args);
}
/**
* Step that installs the default version of Pig on your job flow.
*
* @return HadoopJarStepConfig that can be passed to your job flow.
*/
public HadoopJarStepConfig newInstallPigStep() {
return newInstallPigStep(new String[0]);
}
/**
* Step that installs Pig on your job flow.
*
* @param pigVersions the versions of Pig to install.
*
* @return HadoopJarStepConfig that can be passed to your job flow.
*/
public HadoopJarStepConfig newInstallPigStep(String... pigVersions) {
if (pigVersions != null && pigVersions.length > 0) {
return newHivePigStep("pig", "--install-pig", "--pig-versions",
StringUtils.join(",", pigVersions));
}
return newHivePigStep("pig", "--install-pig", "--pig-versions", "latest");
}
/**
* Step that runs a Pig script on your job flow using the specified Pig version.
*
* @param script
* The script to run.
* @param pigVersion
* The Pig version to use.
* @param scriptArgs
* Arguments that get passed to the script.
* @return HadoopJarStepConfig that can be passed to your job flow.
*/
public HadoopJarStepConfig newRunPigScriptStep(String script,
String pigVersion, String... scriptArgs) {
List<String> pigArgs = new ArrayList<String>();
pigArgs.add("--pig-versions");
pigArgs.add(pigVersion);
pigArgs.add("--run-pig-script");
pigArgs.add("--args");
pigArgs.add("-f");
pigArgs.add(script);
pigArgs.addAll(Arrays.asList(scriptArgs));
return newHivePigStep("pig", pigArgs.toArray(new String[0]));
}
/**
* Step that runs a Pig script on your job flow using the default Pig version.
*
* @param script
* The script to run.
* @param scriptArgs
* Arguments that get passed to the script.
* @return HadoopJarStepConfig that can be passed to your job flow.
*/
public HadoopJarStepConfig newRunPigScriptStep(String script, String... scriptArgs) {
return newRunPigScriptStep(script, "latest", scriptArgs);
}
private HadoopJarStepConfig newHivePigStep(String type, String... args) {
List<String> appArgs = new ArrayList<String>();
appArgs.add("--base-path");
appArgs.add("s3://" + bucket + "/libs/" + type + "/");
appArgs.addAll(Arrays.asList(args));
return newScriptRunnerStep("s3://" + bucket + "/libs/" + type + "/" + type + "-script", appArgs.toArray(new String[0]));
}
}