AWS Glue — Access Workflow Parameters from Within Job

与世无争的帅哥 提交于 2019-12-11 12:55:20

问题


How can I retrieve Glue Workflow parameters from within a glue job?

I have an AWS Glue job of type "python shell" that is triggered periodically from within a glue workflow.

The job's code is to be reused from within a large number of different workflows so I'm looking to retrieve workflow parameters to eliminate the need for redundant jobs.

The AWS Developers guide provides the following tutorial: https://docs.aws.amazon.com/glue/latest/dg/workflow-run-properties-code.html

But I've been unsuccessful in getting the sample code to execute without triggering errors. I suspect that this example may only apply to the scala/ pyspark jobs and not to python shell jobs.

I've tried the following code from within the relevant job

import sys
import boto3
from awsglue.utils import getResolvedOptions

args = getResolvedOptions(sys.argv, ['JOB_NAME','WORKFLOW_NAME', 'WORKFLOW_RUN_ID'])
workflow_name = args['WORKFLOW_NAME']
workflow_run_id = args['WORKFLOW_RUN_ID']
workflow_params = glue_client.get_workflow_run_properties(Name=workflow_name,
                                    RunId=workflow_run_id)["RunProperties"]

print(workflow_name, workflow_run_id, workflow_params)

When I trigger the workflow on demand I receive the following error messages:

> Traceback (most recent call last):
> File "/tmp/runscript.py", line 115, in <module>
> runpy.run_path(temp_file_path, run_name='__main__')
> File "/usr/local/lib/python3.6/runpy.py", line 263, in run_path
> pkg_name=pkg_name, script_name=fname)
> File "/usr/local/lib/python3.6/runpy.py", line 96, in _run_module_code
> mod_name, mod_spec, pkg_name, script_name)
> File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code
> exec(code, run_globals)
> File "/tmp/glue-python-scripts-w4fbwl3n/map_etl_python_shell_test_env.py", line 10, in <module>
> File "/glue/lib/awsglue/utils.py", line 10, in getResolvedOptions
> parsed, extra = parser.parse_known_args(args)
> File "/usr/local/lib/python3.6/argparse.py", line 1766, in parse_known_args
> namespace, args = self._parse_known_args(args, namespace)
> File "/usr/local/lib/python3.6/argparse.py", line 2001, in _parse_known_args
', '.join(required_actions))
> File "/usr/local/lib/python3.6/argparse.py", line 2393, in error
> self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
> File "/usr/local/lib/python3.6/argparse.py", line 2380, in exit
> _sys.exit(status)
> SystemExit: 2
> 
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last):
> File "/tmp/runscript.py", line 134, in <module>
> raise e_type(e_value).with_tracsback(new_stack)
> AttributeError: 'SystemExit' object has no attribute 'with_tracsback'

回答1:


The boto3 library provides you an interesting function

glue = boto3.client(service_name='glue', region_name="my-region")
job = glue.get_job(JobName="my-job-name")

default_parameters = job['Job']['DefaultArguments']
default_parameters[u'--my-parameter']

In this way you should be able to manipulate Glue Job arguments through default_parameter. I'm not sure it works straightaway in the Glue Job, but an external script should be able to handle the Glue Job arguments.



来源:https://stackoverflow.com/questions/56825003/aws-glue-access-workflow-parameters-from-within-job

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!