Repository navigation
Expand file tree
/
Copy pathglue_wrapper.py
More file actions
388 lines (336 loc) · 13.4 KB
/
Copy pathglue_wrapper.py
File metadata and controls
388 lines (336 loc) · 13.4 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
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Purpose
Shows how to use the AWS SDK for Python (Boto3) with AWS Glue to
create and manage crawlers, databases, and jobs.
"""
import logging
from botocore.exceptions import ClientError
logger = logging.getLogger(__name__)
# snippet-start:[python.example_code.glue.GlueWrapper.full]
# snippet-start:[python.example_code.glue.GlueWrapper.decl]
class GlueWrapper:
"""Encapsulates AWS Glue actions."""
def __init__(self, glue_client):
"""
:param glue_client: A Boto3 Glue client.
"""
self.glue_client = glue_client
# snippet-end:[python.example_code.glue.GlueWrapper.decl]
# snippet-start:[python.example_code.glue.GetCrawler]
def get_crawler(self, name):
"""
Gets information about a crawler.
:param name: The name of the crawler to look up.
:return: Data about the crawler.
"""
crawler = None
try:
response = self.glue_client.get_crawler(Name=name)
crawler = response["Crawler"]
except ClientError as err:
if err.response["Error"]["Code"] == "EntityNotFoundException":
logger.info("Crawler %s doesn't exist.", name)
else:
logger.error(
"Couldn't get crawler %s. Here's why: %s: %s",
name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
return crawler
# snippet-end:[python.example_code.glue.GetCrawler]
# snippet-start:[python.example_code.glue.CreateCrawler]
def create_crawler(self, name, role_arn, db_name, db_prefix, s3_target):
"""
Creates a crawler that can crawl the specified target and populate a
database in your AWS Glue Data Catalog with metadata that describes the data
in the target.
:param name: The name of the crawler.
:param role_arn: The Amazon Resource Name (ARN) of an AWS Identity and Access
Management (IAM) role that grants permission to let AWS Glue
access the resources it needs.
:param db_name: The name to give the database that is created by the crawler.
:param db_prefix: The prefix to give any database tables that are created by
the crawler.
:param s3_target: The URL to an S3 bucket that contains data that is
the target of the crawler.
"""
try:
self.glue_client.create_crawler(
Name=name,
Role=role_arn,
DatabaseName=db_name,
TablePrefix=db_prefix,
Targets={"S3Targets": [{"Path": s3_target}]},
)
except ClientError as err:
logger.error(
"Couldn't create crawler. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.glue.CreateCrawler]
# snippet-start:[python.example_code.glue.StartCrawler]
def start_crawler(self, name):
"""
Starts a crawler. The crawler crawls its configured target and creates
metadata that describes the data it finds in the target data source.
:param name: The name of the crawler to start.
"""
try:
self.glue_client.start_crawler(Name=name)
except ClientError as err:
logger.error(
"Couldn't start crawler %s. Here's why: %s: %s",
name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.glue.StartCrawler]
# snippet-start:[python.example_code.glue.GetDatabase]
def get_database(self, name):
"""
Gets information about a database in your Data Catalog.
:param name: The name of the database to look up.
:return: Information about the database.
"""
try:
response = self.glue_client.get_database(Name=name)
except ClientError as err:
logger.error(
"Couldn't get database %s. Here's why: %s: %s",
name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return response["Database"]
# snippet-end:[python.example_code.glue.GetDatabase]
# snippet-start:[python.example_code.glue.GetTables]
def get_tables(self, db_name):
"""
Gets a list of tables in a Data Catalog database.
:param db_name: The name of the database to query.
:return: The list of tables in the database.
"""
try:
response = self.glue_client.get_tables(DatabaseName=db_name)
except ClientError as err:
logger.error(
"Couldn't get tables %s. Here's why: %s: %s",
db_name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return response["TableList"]
# snippet-end:[python.example_code.glue.GetTables]
# snippet-start:[python.example_code.glue.CreateJob]
def create_job(self, name, description, role_arn, script_location):
"""
Creates a job definition for an extract, transform, and load (ETL) job that can
be run by AWS Glue.
:param name: The name of the job definition.
:param description: The description of the job definition.
:param role_arn: The ARN of an IAM role that grants AWS Glue the permissions
it requires to run the job.
:param script_location: The Amazon S3 URL of a Python ETL script that is run as
part of the job. The script defines how the data is
transformed.
"""
try:
self.glue_client.create_job(
Name=name,
Description=description,
Role=role_arn,
Command={
"Name": "glueetl",
"ScriptLocation": script_location,
"PythonVersion": "3",
},
GlueVersion="3.0",
)
except ClientError as err:
logger.error(
"Couldn't create job %s. Here's why: %s: %s",
name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.glue.CreateJob]
# snippet-start:[python.example_code.glue.StartJobRun]
def start_job_run(self, name, input_database, input_table, output_bucket_name):
"""
Starts a job run. A job run extracts data from the source, transforms it,
and loads it to the output bucket.
:param name: The name of the job definition.
:param input_database: The name of the metadata database that contains tables
that describe the source data. This is typically created
by a crawler.
:param input_table: The name of the table in the metadata database that
describes the source data.
:param output_bucket_name: The S3 bucket where the output is written.
:return: The ID of the job run.
"""
try:
# The custom Arguments that are passed to this function are used by the
# Python ETL script to determine the location of input and output data.
response = self.glue_client.start_job_run(
JobName=name,
Arguments={
"--input_database": input_database,
"--input_table": input_table,
"--output_bucket_url": f"s3://{output_bucket_name}/",
},
)
except ClientError as err:
logger.error(
"Couldn't start job run %s. Here's why: %s: %s",
name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return response["JobRunId"]
# snippet-end:[python.example_code.glue.StartJobRun]
# snippet-start:[python.example_code.glue.ListJobs]
def list_jobs(self):
"""
Lists the names of job definitions in your account.
:return: The list of job definition names.
"""
try:
response = self.glue_client.list_jobs()
except ClientError as err:
logger.error(
"Couldn't list jobs. Here's why: %s: %s",
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return response["JobNames"]
# snippet-end:[python.example_code.glue.ListJobs]
# snippet-start:[python.example_code.glue.GetJobRuns]
def get_job_runs(self, job_name):
"""
Gets information about runs that have been performed for a specific job
definition.
:param job_name: The name of the job definition to look up.
:return: The list of job runs.
"""
try:
response = self.glue_client.get_job_runs(JobName=job_name)
except ClientError as err:
logger.error(
"Couldn't get job runs for %s. Here's why: %s: %s",
job_name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return response["JobRuns"]
# snippet-end:[python.example_code.glue.GetJobRuns]
# snippet-start:[python.example_code.glue.GetJobRun]
def get_job_run(self, name, run_id):
"""
Gets information about a single job run.
:param name: The name of the job definition for the run.
:param run_id: The ID of the run.
:return: Information about the run.
"""
try:
response = self.glue_client.get_job_run(JobName=name, RunId=run_id)
except ClientError as err:
logger.error(
"Couldn't get job run %s/%s. Here's why: %s: %s",
name,
run_id,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
else:
return response["JobRun"]
# snippet-end:[python.example_code.glue.GetJobRun]
# snippet-start:[python.example_code.glue.DeleteJob]
def delete_job(self, job_name):
"""
Deletes a job definition. This also deletes data about all runs that are
associated with this job definition.
:param job_name: The name of the job definition to delete.
"""
try:
self.glue_client.delete_job(JobName=job_name)
except ClientError as err:
logger.error(
"Couldn't delete job %s. Here's why: %s: %s",
job_name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.glue.DeleteJob]
# snippet-start:[python.example_code.glue.DeleteTable]
def delete_table(self, db_name, table_name):
"""
Deletes a table from a metadata database.
:param db_name: The name of the database that contains the table.
:param table_name: The name of the table to delete.
"""
try:
self.glue_client.delete_table(DatabaseName=db_name, Name=table_name)
except ClientError as err:
logger.error(
"Couldn't delete table %s. Here's why: %s: %s",
table_name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.glue.DeleteTable]
# snippet-start:[python.example_code.glue.DeleteDatabase]
def delete_database(self, name):
"""
Deletes a metadata database from your Data Catalog.
:param name: The name of the database to delete.
"""
try:
self.glue_client.delete_database(Name=name)
except ClientError as err:
logger.error(
"Couldn't delete database %s. Here's why: %s: %s",
name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.glue.DeleteDatabase]
# snippet-start:[python.example_code.glue.DeleteCrawler]
def delete_crawler(self, name):
"""
Deletes a crawler.
:param name: The name of the crawler to delete.
"""
try:
self.glue_client.delete_crawler(Name=name)
except ClientError as err:
logger.error(
"Couldn't delete crawler %s. Here's why: %s: %s",
name,
err.response["Error"]["Code"],
err.response["Error"]["Message"],
)
raise
# snippet-end:[python.example_code.glue.DeleteCrawler]
# snippet-end:[python.example_code.glue.GlueWrapper.full]