如何从粘合作业中检索粘合工作流参数?
我有一个类型为“ python shell”的AWS Glue作业,该作业会从粘合工作流程中定期触发。
该作业的代码将在许多不同的工作流程中重用,因此我希望检索工作流程参数以消除对冗余作业的需求。
AWS开发人员指南提供了以下教程: https://docs.aws.amazon.com/glue/latest/dg/workflow-run-properties-code.html
但是我一直未能成功执行示例代码而不会触发错误。我怀疑该示例可能仅适用于scala / pyspark作业,而不适用于python shell作业。
我从相关工作中尝试了以下代码
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)
当我按需触发工作流时,会收到以下错误消息:
> 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'
答案 0 :(得分:0)
boto3
库为您提供了有趣的功能
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']
通过这种方式,您应该能够通过default_parameter
处理Glue Job参数。我不确定它可以直接在Glue Job中工作,但是外部脚本应该能够处理Glue Job参数。
答案 1 :(得分:0)
在工作流中运行上述代码会产生错误:
usage: workflow-test.py [-h] --JOB_NAME JOB_NAME --WORKFLOW_NAME WORKFLOW_NAME
--WORKFLOW_RUN_ID WORKFLOW_RUN_ID
workflow-test.py: error: the following arguments are required: --JOB_NAME
其次是上面粘贴的内容。
似乎AWS文档已经过时,并且JOB_NAME
参数未在属于工作流的作业中使用。
您将通过以下方式获取工作流程参数:
args = getResolvedOptions(sys.argv, ['WORKFLOW_NAME', 'WORKFLOW_RUN_ID'])