-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathjob-executable.ts
393 lines (346 loc) · 10.9 KB
/
job-executable.ts
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
import { Code } from './code';
/**
* AWS Glue version determines the versions of Apache Spark and Python that are available to the job.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/add-job.html.
*
* If you need to use a GlueVersion that doesn't exist as a static member, you
* can instantiate a `GlueVersion` object, e.g: `GlueVersion.of('1.5')`.
*/
export class GlueVersion {
/**
* Glue version using Spark 2.2.1 and Python 2.7
*/
public static readonly V0_9 = new GlueVersion('0.9');
/**
* Glue version using Spark 2.4.3, Python 2.7 and Python 3.6
*/
public static readonly V1_0 = new GlueVersion('1.0');
/**
* Glue version using Spark 2.4.3 and Python 3.7
*/
public static readonly V2_0 = new GlueVersion('2.0');
/**
* Glue version using Spark 3.1.1 and Python 3.7
*/
public static readonly V3_0 = new GlueVersion('3.0');
/**
* Custom Glue version
* @param version custom version
*/
public static of(version: string): GlueVersion {
return new GlueVersion(version);
}
/**
* The name of this GlueVersion, as expected by Job resource.
*/
public readonly name: string;
private constructor(name: string) {
this.name = name;
}
}
/**
* Runtime language of the Glue job
*/
export enum JobLanguage {
/**
* Scala
*/
SCALA = 'scala',
/**
* Python
*/
PYTHON = 'python',
}
/**
* Python version
*/
export enum PythonVersion {
/**
* Python 2 (the exact version depends on GlueVersion and JobCommand used)
*/
TWO = '2',
/**
* Python 3 (the exact version depends on GlueVersion and JobCommand used)
*/
THREE = '3',
}
/**
* The job type.
*
* If you need to use a JobType that doesn't exist as a static member, you
* can instantiate a `JobType` object, e.g: `JobType.of('other name')`.
*/
export class JobType {
/**
* Command for running a Glue ETL job.
*/
public static readonly ETL = new JobType('glueetl');
/**
* Command for running a Glue streaming job.
*/
public static readonly STREAMING = new JobType('gluestreaming');
/**
* Command for running a Glue python shell job.
*/
public static readonly PYTHON_SHELL = new JobType('pythonshell');
/**
* Custom type name
* @param name type name
*/
public static of(name: string): JobType {
return new JobType(name);
}
/**
* The name of this JobType, as expected by Job resource.
*/
public readonly name: string;
private constructor(name: string) {
this.name = name;
}
}
interface PythonExecutableProps {
/**
* The Python version to use.
*/
readonly pythonVersion: PythonVersion;
/**
* Additional Python files that AWS Glue adds to the Python path before executing your script.
* Only individual files are supported, directories are not supported.
*
* @default - no extra python files and argument is not set
*
* @see `--extra-py-files` in https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly extraPythonFiles?: Code[];
}
interface SharedJobExecutableProps {
/**
* Glue version.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/release-notes.html
*/
readonly glueVersion: GlueVersion;
/**
* The script that executes a job.
*/
readonly script: Code;
/**
* Additional files, such as configuration files that AWS Glue copies to the working directory of your script before executing it.
* Only individual files are supported, directories are not supported.
*
* @default [] - no extra files are copied to the working directory
*
* @see `--extra-files` in https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly extraFiles?: Code[];
}
interface SharedSparkJobExecutableProps extends SharedJobExecutableProps {
/**
* Additional Java .jar files that AWS Glue adds to the Java classpath before executing your script.
* Only individual files are supported, directories are not supported.
*
* @default [] - no extra jars are added to the classpath
*
* @see `--extra-jars` in https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly extraJars?: Code[];
/**
* Setting this value to true prioritizes the customer's extra JAR files in the classpath.
*
* @default false - priority is not given to user-provided jars
*
* @see `--user-jars-first` in https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly extraJarsFirst?: boolean;
}
/**
* Props for creating a Scala Spark (ETL or Streaming) job executable
*/
export interface ScalaJobExecutableProps extends SharedSparkJobExecutableProps {
/**
* The fully qualified Scala class name that serves as the entry point for the job.
*
* @see `--class` in https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly className: string;
}
/**
* Props for creating a Python Spark (ETL or Streaming) job executable
*/
export interface PythonSparkJobExecutableProps extends SharedSparkJobExecutableProps, PythonExecutableProps {}
/**
* Props for creating a Python shell job executable
*/
export interface PythonShellExecutableProps extends SharedJobExecutableProps, PythonExecutableProps {}
/**
* The executable properties related to the Glue job's GlueVersion, JobType and code
*/
export class JobExecutable {
/**
* Create Scala executable props for Apache Spark ETL job.
*
* @param props Scala Apache Spark Job props
*/
public static scalaEtl(props: ScalaJobExecutableProps): JobExecutable {
return new JobExecutable({
...props,
type: JobType.ETL,
language: JobLanguage.SCALA,
});
}
/**
* Create Scala executable props for Apache Spark Streaming job.
*
* @param props Scala Apache Spark Job props
*/
public static scalaStreaming(props: ScalaJobExecutableProps): JobExecutable {
return new JobExecutable({
...props,
type: JobType.STREAMING,
language: JobLanguage.SCALA,
});
}
/**
* Create Python executable props for Apache Spark ETL job.
*
* @param props Python Apache Spark Job props
*/
public static pythonEtl(props: PythonSparkJobExecutableProps): JobExecutable {
return new JobExecutable({
...props,
type: JobType.ETL,
language: JobLanguage.PYTHON,
});
}
/**
* Create Python executable props for Apache Spark Streaming job.
*
* @param props Python Apache Spark Job props
*/
public static pythonStreaming(props: PythonSparkJobExecutableProps): JobExecutable {
return new JobExecutable({
...props,
type: JobType.STREAMING,
language: JobLanguage.PYTHON,
});
}
/**
* Create Python executable props for python shell jobs.
*
* @param props Python Shell Job props.
*/
public static pythonShell(props: PythonShellExecutableProps): JobExecutable {
return new JobExecutable({
...props,
type: JobType.PYTHON_SHELL,
language: JobLanguage.PYTHON,
});
}
/**
* Create a custom JobExecutable.
*
* @param config custom job executable configuration.
*/
public static of(config: JobExecutableConfig): JobExecutable {
return new JobExecutable(config);
}
private config: JobExecutableConfig;
private constructor(config: JobExecutableConfig) {
if (JobType.PYTHON_SHELL === config.type) {
if (config.language !== JobLanguage.PYTHON) {
throw new Error('Python shell requires the language to be set to Python');
}
if ([GlueVersion.V0_9, GlueVersion.V2_0, GlueVersion.V3_0].includes(config.glueVersion)) {
throw new Error(`Specified GlueVersion ${config.glueVersion.name} does not support Python Shell`);
}
}
if (config.extraJarsFirst && [GlueVersion.V0_9, GlueVersion.V1_0].includes(config.glueVersion)) {
throw new Error(`Specified GlueVersion ${config.glueVersion.name} does not support extraJarsFirst`);
}
if (config.pythonVersion === PythonVersion.TWO && ![GlueVersion.V0_9, GlueVersion.V1_0].includes(config.glueVersion)) {
throw new Error(`Specified GlueVersion ${config.glueVersion.name} does not support PythonVersion ${config.pythonVersion}`);
}
if (JobLanguage.PYTHON !== config.language && config.extraPythonFiles) {
throw new Error('extraPythonFiles is not supported for languages other than JobLanguage.PYTHON');
}
this.config = config;
}
/**
* Called during Job initialization to get JobExecutableConfig.
*/
public bind(): JobExecutableConfig {
return this.config;
}
}
/**
* Result of binding a `JobExecutable` into a `Job`.
*/
export interface JobExecutableConfig {
/**
* Glue version.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/release-notes.html
*/
readonly glueVersion: GlueVersion;
/**
* The language of the job (Scala or Python).
*
* @see `--job-language` in https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly language: JobLanguage;
/**
* Specify the type of the job whether it's an Apache Spark ETL or streaming one or if it's a Python shell job.
*/
readonly type: JobType;
/**
* The Python version to use.
*
* @default - no python version specified
*/
readonly pythonVersion?: PythonVersion;
/**
* The script that is executed by a job.
*/
readonly script: Code;
/**
* The Scala class that serves as the entry point for the job. This applies only if your the job langauage is Scala.
*
* @default - no scala className specified
*
* @see `--class` in https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly className?: string;
/**
* Additional Java .jar files that AWS Glue adds to the Java classpath before executing your script.
*
* @default - no extra jars specified.
*
* @see `--extra-jars` in https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly extraJars?: Code[];
/**
* Additional Python files that AWS Glue adds to the Python path before executing your script.
*
* @default - no extra python files specified.
*
* @see `--extra-py-files` in https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly extraPythonFiles?: Code[];
/**
* Additional files, such as configuration files that AWS Glue copies to the working directory of your script before executing it.
*
* @default - no extra files specified.
*
* @see `--extra-files` in https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly extraFiles?: Code[];
/**
* Setting this value to true prioritizes the customer's extra JAR files in the classpath.
*
* @default - extra jars are not prioritized.
*
* @see `--user-jars-first` in https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly extraJarsFirst?: boolean;
}