猿问

如何使用 python 触发 aws lambda 的粘合作业?

假设我有一个胶水作业,名为:FirstGlueJob

如何在 python 中使用 lambda 函数触发它?


慕少森
浏览 89回答 1
1回答

千万里不及你

我们可以在着陆文件夹上配置一个 Lambda S3 事件触发器,当文件上传时,我们可以在 Lambda 中有一个简短的脚本来触发 Glue 作业。胶水 python 脚本应该具有将输入文本文件转换为 CSV 文件所需的逻辑。这样,当文件上传到 S3 时,您的作业可以运行任意次数。您的账单也只在作业运行期间计费。请注意,由于其托管服务功能,Glue 的成本并不高。创建事件触发器,触发胶水作业。请在此处找到 AWS Lambda 的代码片段:from __future__ import print_functionimport jsonimport boto3import timeimport sysimport timefrom datetime import datetimes3 = boto3.client('s3')glue = boto3.client('glue')def lambda_handler(event, context):&nbsp; &nbsp; gluejobname="<< THE GLUE JOB NAME >>"&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; runId = glue.start_job_run(JobName=gluejobname)&nbsp; &nbsp; &nbsp; &nbsp; status = glue.get_job_run(JobName=gluejobname, RunId=runId['JobRunId'])&nbsp; &nbsp; &nbsp; &nbsp; print("Job Status : ", status['JobRun']['JobRunState'])&nbsp; &nbsp; except Exception as e:&nbsp; &nbsp; &nbsp; &nbsp; print(e)&nbsp; &nbsp; &nbsp; &nbsp; print('Error getting object {} from bucket {}. Make sure they exist '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'and your bucket is in the same region as this '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'function.'.format(source_bucket, source_bucket))&nbsp; &nbsp; raise e
随时随地看视频慕课网APP

相关分类

Python
我要回答