forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootstrapActions.java
More file actions
249 lines (222 loc) · 7.79 KB
/
BootstrapActions.java
File metadata and controls
249 lines (222 loc) · 7.79 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
/*
* Copyright 2011-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.List;
import com.amazonaws.services.elasticmapreduce.model.BootstrapActionConfig;
import com.amazonaws.services.elasticmapreduce.model.ScriptBootstrapActionConfig;
/**
* Class that provides helper methods for constructing predefined bootstrap actions.
*
* <pre>
* AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
* AmazonElasticMapReduce emr = new AmazonElasticMapReduceClient(credentials);
*
* BootstrapActions bootstrapActions = new BootstrapActions();
*
* RunJobFlowRequest request = new RunJobFlowRequest()
* .withName("Job Flow With Bootstrap Actions")
* .withBootstrapActions(
* bootstrapActions.newRunIf(
* "instance.isMaster=true",
* bootstrapActions.newConfigureDaemons()
* .withHeapSize(Daemon.JobTracker, 2048)
* .build()))
* .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 BootstrapActions {
private final String bucket;
/**
* Creates a new default BootstrapActions for us in us-east-1.
*/
public BootstrapActions() {
this("us-east-1.elasticmapreduce");
}
/**
* Creates a new BootstrapActions.
* @param bucket the bucket from which to download the bootstrap actions.
*/
public BootstrapActions(String bucket) {
this.bucket = bucket;
}
/**
* Create a new run-if bootstrap action which lets you conditionally run bootstrap actions.
* @param condition The condition to evaluate, if true the bootstrap action executes.
* @param config The bootstrap action to execute in case of successful evaluation.
* @return A BootstrapActionConfig to be provided when running a job flow.
*/
public BootstrapActionConfig newRunIf(String condition, BootstrapActionConfig config) {
List<String> args = config.getScriptBootstrapAction().getArgs();
args.add(0, condition);
args.add(1, config.getScriptBootstrapAction().getPath());
return new BootstrapActionConfig()
.withName("Run If, " + config.getName())
.withScriptBootstrapAction(new ScriptBootstrapActionConfig()
.withPath("s3://" + bucket + "/bootstrap-actions/run-if")
.withArgs(args));
}
/**
* Enum specifying all valid config files.
*/
public enum ConfigFile {
Site,
Default,
Core,
Hdfs,
Mapred
}
/**
* Create a new bootstrap action which lets you configure Hadoop's XML files.
*/
public ConfigureHadoop newConfigureHadoop() {
return new ConfigureHadoop();
}
public class ConfigureHadoop {
List<String> args = new ArrayList<String>();
private ConfigureHadoop() {
}
/**
* Specify an XML file in S3 to merge with Hadoop's default configuration.
* @param file The config file to merge with.
* @param xmlPath The path in S3 of the XML file.
* @return A reference to this updated object so that method calls can be chained
* together.
*/
public ConfigureHadoop withXml(ConfigFile file, String xmlPath) {
String arg = "";
switch (file) {
case Site: arg = "-S"; break;
case Default: arg = "-D"; break;
case Core: arg = "-C"; break;
case Hdfs: arg = "-H"; break;
case Mapred: arg = "-M"; break;
}
args.add(arg);
args.add(xmlPath);
return this;
}
/**
* Specify a key-value pair to merge with Hadoop's default configuration.
* @param file The config file to merge with.
* @param key The config key.
* @param value The config value.
* @return A reference to this updated object so that method calls can be chained
* together.
*/
public ConfigureHadoop withKeyValue(ConfigFile file, String key, String value) {
String arg = "";
switch (file) {
case Site: arg = "-s"; break;
case Default: arg = "-d"; break;
case Core: arg = "-c"; break;
case Hdfs: arg = "-h"; break;
case Mapred: arg = "-m"; break;
}
args.add(arg);
args.add(key + "=" + value);
return this;
}
/**
* Returns an object which can be used in a RunJobflow call.
* @return an object which can be used in a RunJobflow call.
*/
public BootstrapActionConfig build() {
return new BootstrapActionConfig()
.withName("Configure Hadoop")
.withScriptBootstrapAction(new ScriptBootstrapActionConfig()
.withPath("s3://" + bucket + "/bootstrap-actions/configure-hadoop")
.withArgs(args));
}
}
/**
* List of Hadoop daemons which can be configured.
*/
public enum Daemon {
NameNode,
DataNode,
JobTracker,
TaskTracker,
Client;
}
/**
* Create a new bootstrap action which lets you configure Hadoop's daemons. The options
* are written to the hadoop-user-env.sh file.
*/
public ConfigureDaemons newConfigureDaemons() {
return new ConfigureDaemons();
}
public class ConfigureDaemons {
List<String> args = new ArrayList<String>();
boolean replace = false;
private ConfigureDaemons() {
}
/**
* Set the heap size of a daemon.
* @param daemon The deamon to configure.
* @param megabytes The requested heap size of the daemon.
* @return A reference to this updated object so that method calls can be chained
* together.
*/
public ConfigureDaemons withHeapSize(Daemon daemon, int megabytes) {
args.add("--" + daemon.name().toLowerCase() + "-heap-size=" + megabytes);
return this;
}
/**
* Specify additional Java opts to be included when the daemon starts.
* @param daemon The daemon to add opts to.
* @param opts Additional Java command line arguments.
* @return A reference to this updated object so that method calls can be chained
* together.
*/
public ConfigureDaemons withOpts(Daemon daemon, String opts) {
args.add("--" + daemon.name().toLowerCase() + "-opts=\"" + opts + "\"");
return this;
}
/**
* Replace the existing hadoop-user-env.sh file if it exists.
* @param replace whether the file should be replaced.
* @return A reference to this updated object so that method calls can be chained
* together.
*/
public ConfigureDaemons withReplace(boolean replace) {
this.replace = replace;
return this;
}
/**
* Returns an object which can be used in a RunJobflow call.
* @return an object which can be used in a RunJobflow call.
*/
public BootstrapActionConfig build() {
if (replace) {
args.add("--replace");
}
return new BootstrapActionConfig()
.withName("Configure Daemons")
.withScriptBootstrapAction(new ScriptBootstrapActionConfig()
.withPath("s3://" + bucket + "/bootstrap-actions/configure-daemons")
.withArgs(args));
}
}
}