-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
job-executable.ts
527 lines (466 loc) · 14.7 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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
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');
/**
* Glue version using Spark 3.3.0 and Python 3.10
*/
public static readonly V4_0 = new GlueVersion('4.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',
/**
* Python 3.9 (the exact version depends on GlueVersion and JobCommand used)
*/
THREE_NINE = '3.9',
}
/**
* AWS Glue runtime determines the runtime engine of the job.
*
*/
export class Runtime {
/**
* Runtime for a Glue for Ray 2.4.
*/
public static readonly RAY_TWO_FOUR = new Runtime('Ray2.4');
/**
* Custom runtime
* @param runtime custom runtime
*/
public static of(runtime: string): Runtime {
return new Runtime(runtime);
}
/**
* The name of this Runtime.
*/
public readonly name: string;
private constructor(name: string) {
this.name = name;
}
}
/**
* 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 Spark job.
*/
public static readonly ETL = new JobType('glueetl');
/**
* Command for running a Glue Spark 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');
/**
* Command for running a Glue Ray job.
*/
public static readonly RAY = new JobType('glueray');
/**
* 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.
* Equivalent to a job parameter `--extra-py-files`.
*
* @default - no extra python files and argument is not set
*
* @see https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly extraPythonFiles?: Code[];
}
interface RayExecutableProps {
/**
* The Python version to use.
*/
readonly pythonVersion: PythonVersion;
/**
* Additional Python modules that AWS Glue adds to the Python path before executing your script.
* Equivalent to a job parameter `--s3-py-modules`.
*
* @default - no extra python files and argument is not set
*
* @see https://docs.aws.amazon.com/glue/latest/dg/author-job-ray-job-parameters.html
*/
readonly s3PythonModules?: Code[];
}
interface SharedJobExecutableProps {
/**
* Runtime. It is required for Ray jobs.
*
*/
readonly runtime?: Runtime;
/**
* 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.
* Equivalent to a job parameter `--extra-files`.
*
* @default [] - no extra files are copied to the working directory
*
* @see 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.
* Equivalent to a job parameter `--extra-jars`.
*
* @default [] - no extra jars are added to the classpath
*
* @see 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.
* Equivalent to a job parameter `--user-jars-first`.
*
* @default false - priority is not given to user-provided jars
*
* @see 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.
* Equivalent to a job parameter `--class`.
*
* @see 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 {}
/**
* Props for creating a Python Ray job executable
*/
export interface PythonRayExecutableProps extends SharedJobExecutableProps, RayExecutableProps {}
/**
* 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 Python executable props for Ray jobs.
*
* @param props Ray Job props.
*/
public static pythonRay(props: PythonRayExecutableProps): JobExecutable {
return new JobExecutable({
...props,
type: JobType.RAY,
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) {
const glueVersion = config.glueVersion.name;
const type = config.type.name;
if (JobType.PYTHON_SHELL.name === type) {
if (config.language !== JobLanguage.PYTHON) {
throw new Error('Python shell requires the language to be set to Python');
}
if ([GlueVersion.V0_9.name, GlueVersion.V4_0.name].includes(glueVersion)) {
throw new Error(`Specified GlueVersion ${glueVersion} does not support Python Shell`);
}
}
if (JobType.RAY.name === type) {
if (config.language !== JobLanguage.PYTHON) {
throw new Error('Ray requires the language to be set to Python');
}
if ([GlueVersion.V0_9.name, GlueVersion.V1_0.name, GlueVersion.V2_0.name, GlueVersion.V3_0.name].includes(glueVersion)) {
throw new Error(`Specified GlueVersion ${glueVersion} does not support Ray`);
}
}
if (config.extraJarsFirst && [GlueVersion.V0_9.name, GlueVersion.V1_0.name].includes(glueVersion)) {
throw new Error(`Specified GlueVersion ${glueVersion} does not support extraJarsFirst`);
}
if (config.pythonVersion === PythonVersion.TWO && ![GlueVersion.V0_9.name, GlueVersion.V1_0.name].includes(glueVersion)) {
throw new Error(`Specified GlueVersion ${glueVersion} 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');
}
if (config.extraPythonFiles && type === JobType.RAY.name) {
throw new Error('extraPythonFiles is not supported for Ray jobs');
}
if (config.pythonVersion === PythonVersion.THREE_NINE && type !== JobType.PYTHON_SHELL.name && type !== JobType.RAY.name) {
throw new Error('Specified PythonVersion PythonVersion.THREE_NINE is only supported for JobType Python Shell and Ray');
}
if (config.pythonVersion === PythonVersion.THREE && type === JobType.RAY.name) {
throw new Error('Specified PythonVersion PythonVersion.THREE is not supported for Ray');
}
if (config.runtime === undefined && type === JobType.RAY.name) {
throw new Error('Runtime is required for Ray jobs');
}
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).
* Equivalent to a job parameter `--job-language`.
*
* @see 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 Runtime to use.
*
* @default - no runtime specified
*/
readonly runtime?: Runtime;
/**
* 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.
* Equivalent to a job parameter `--class`.
*
* @default - no scala className specified
*
* @see 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.
* Equivalent to a job parameter `--extra-jars`.
*
* @default - no extra jars specified.
*
* @see 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.
* Equivalent to a job parameter `--extra-py-files`.
*
* @default - no extra python files specified.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly extraPythonFiles?: Code[];
/**
* Additional Python modules that AWS Glue adds to the Python path before executing your script.
* Equivalent to a job parameter `--s3-py-modules`.
*
* @default - no extra python files specified.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/author-job-ray-job-parameters.html
*/
readonly s3PythonModules?: Code[];
/**
* Additional files, such as configuration files that AWS Glue copies to the working directory of your script before executing it.
* Equivalent to a job parameter `--extra-files`.
*
* @default - no extra files specified.
*
* @see 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.
* Equivalent to a job parameter `--user-jars-first`.
*
* @default - extra jars are not prioritized.
*
* @see https://docs.aws.amazon.com/glue/latest/dg/aws-glue-programming-etl-glue-arguments.html
*/
readonly extraJarsFirst?: boolean;
}