[英]Optional job parameter in AWS Glue?
How can I implement an optional parameter to an AWS Glue Job?如何为 AWS Glue 作业实施可选参数?
I have created a job that currently have a string parameter (an ISO 8601 date string) as an input that is used in the ETL job.我创建了一个当前具有字符串参数(ISO 8601 日期字符串)作为 ETL 作业中使用的输入的作业。 I would like to make this parameter optional, so that the job use a default value if it is not provided (eg using datetime.now and datetime.isoformat in my case).
我想将此参数设为可选,以便作业在未提供时使用默认值(例如,在我的情况下使用datetime.now和datetime.isoformat )。 I have tried using getResolvedOptions :
我试过使用getResolvedOptions :
import sys
from awsglue.utils import getResolvedOptions
args = getResolvedOptions(sys.argv, ['ISO_8601_STRING'])
However, when I am not passing an --ISO_8601_STRING
job parameter I see the following error:但是,当我没有传递
--ISO_8601_STRING
作业参数时,我看到以下错误:
awsglue.utils.GlueArgumentError: argument --ISO_8601_STRING is required
awsglue.utils.GlueArgumentError:参数 --ISO_8601_STRING 是必需的
matsev and Yuriy solutions is fine if you have only one field which is optional.如果您只有一个可选字段, matsev和Yuriy解决方案就很好。
I wrote a wrapper function for python that is more generic and handle different corner cases (mandatory fields and/or optional fields with values).我为 python 编写了一个更通用的包装函数,可以处理不同的极端情况(必填字段和/或带有值的可选字段)。
import sys
from awsglue.utils import getResolvedOptions
def get_glue_args(mandatory_fields, default_optional_args):
"""
This is a wrapper of the glue function getResolvedOptions to take care of the following case :
* Handling optional arguments and/or mandatory arguments
* Optional arguments with default value
NOTE:
* DO NOT USE '-' while defining args as the getResolvedOptions with replace them with '_'
* All fields would be return as a string type with getResolvedOptions
Arguments:
mandatory_fields {list} -- list of mandatory fields for the job
default_optional_args {dict} -- dict for optional fields with their default value
Returns:
dict -- given args with default value of optional args not filled
"""
# The glue args are available in sys.argv with an extra '--'
given_optional_fields_key = list(set([i[2:] for i in sys.argv]).intersection([i for i in default_optional_args]))
args = getResolvedOptions(sys.argv,
mandatory_fields+given_optional_fields_key)
# Overwrite default value if optional args are provided
default_optional_args.update(args)
return default_optional_args
Usage :用法 :
# Defining mandatory/optional args
mandatory_fields = ['my_mandatory_field_1','my_mandatory_field_2']
default_optional_args = {'optional_field_1':'myvalue1', 'optional_field_2':'myvalue2'}
# Retrieve args
args = get_glue_args(mandatory_fields, default_optional_args)
# Access element as dict with args[‘key’]
There is a workaround to have optional parameters.有一种解决方法可以使用可选参数。 The idea is to examine arguments before resolving them (Scala):
这个想法是在解决它们之前检查参数(Scala):
val argName = 'ISO_8601_STRING'
var argValue = null
if (sysArgs.contains(s"--$argName"))
argValue = GlueArgParser.getResolvedOptions(sysArgs, Array(argName))(argName)
Porting Yuriy's answer to Python solved my problem:移植Yuriy对 Python的回答解决了我的问题:
if ('--{}'.format('ISO_8601_STRING') in sys.argv):
args = getResolvedOptions(sys.argv, ['ISO_8601_STRING'])
else:
args = {'ISO_8601_STRING': datetime.datetime.now().isoformat()}
我没有看到有可选参数的方法,但是您可以在作业本身上指定默认参数,然后如果您在运行作业时不传递该参数,您的作业将收到默认值(注意默认值不能为空)。
Wrapping matsev's answer in a function:将matsev 的答案包装在一个函数中:
def get_glue_env_var(key, default="none"):
if f'--{key}' in sys.argv:
return getResolvedOptions(sys.argv, [key])[key]
else:
return default
It's possible to create a Step Function that starts the same Glue job with different parameters.可以创建一个 Step Function 来启动具有不同参数的相同 Glue 作业。 The state machine starts with a Choice state and uses different number of inputs depending on which is present.
状态机以选择状态开始,并根据存在的情况使用不同数量的输入。
stepFunctions:
stateMachines:
taskMachine:
role:
Fn::GetAtt: [ TaskExecutor, Arn ]
name: ${self:service}-${opt:stage}
definition:
StartAt: DefaultOrNot
States:
DefaultOrNot:
Type: Choice
Choices:
- Variable: "$.optional_input"
IsPresent: false
Next: DefaultTask
- Variable: "$. optional_input"
IsPresent: true
Next: OptionalTask
OptionalTask:
Type: Task
Resource: "arn:aws:states:::glue:startJobRun.task0"
Parameters:
JobName: ${self:service}-${opt:stage}
Arguments:
'--log_group.$': "$.specs.log_group"
'--log_stream.$': "$.specs.log_stream"
'--optional_input.$': "$. optional_input"
Catch:
- ErrorEquals: [ 'States.TaskFailed' ]
ResultPath: "$.errorInfo"
Next: TaskFailed
Next: ExitExecution
DefaultTask:
Type: Task
Resource: "arn:aws:states:::glue:startJobRun.sync"
Parameters:
JobName: ${self:service}-${opt:stage}
Arguments:
'--log_group.$': "$.specs.log_group"
'--log_stream.$': "$.specs.log_stream"
Catch:
- ErrorEquals: [ 'States.TaskFailed' ]
ResultPath: "$.errorInfo"
Next: TaskFailed
Next: ExitExecution
TaskFailed:
Type: Fail
Error: "Failure"
ExitExecution:
Type: Pass
End: True
If you're using the interface, you must provide your parameter names starting with "--" like "--TABLE_NAME", rather than "TABLE_NAME", then you can use them like the following (python) code:如果您使用该接口,则必须提供以“--”开头的参数名称,例如“--TABLE_NAME”,而不是“TABLE_NAME”,然后您可以像以下(python)代码一样使用它们:
args = getResolvedOptions(sys.argv, ['JOB_NAME', 'TABLE_NAME'])
table_name = args['TABLE_NAME']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.