为了账号安全,请及时绑定邮箱和手机立即绑定

AWS Glue 中的可选作业参数?

AWS Glue 中的可选作业参数?

人到中年有点甜 2021-06-07 01:29:53
如何为 AWS Glue 作业实施可选参数?我创建了一个当前具有字符串参数(ISO 8601 日期字符串)作为 ETL 作业中使用的输入的作业。我想将此参数设为可选,以便作业在未提供时使用默认值(例如,在我的情况下使用datetime.now和datetime.isoformat)。我曾尝试使用getResolvedOptions:import sysfrom awsglue.utils import getResolvedOptionsargs = getResolvedOptions(sys.argv, ['ISO_8601_STRING'])但是,当我没有传递--ISO_8601_STRING作业参数时,我看到以下错误:awsglue.utils.GlueArgumentError:需要参数 --ISO_8601_STRING
查看完整描述

3 回答

?
蓝山帝景

TA贡献1843条经验 获得超7个赞

如果您只有一个可选字段,matsev和Yuriy解决方案就很好。


我为 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

用法 :


# 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’]


查看完整回答
反对 回复 2021-06-09
?
墨色风雨

TA贡献1853条经验 获得超6个赞

移植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()}


查看完整回答
反对 回复 2021-06-09
?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

有一种解决方法可以使用可选参数。这个想法是在解决它们之前检查参数(Scala):


val argName = 'ISO_8601_STRING'

var argValue = null

if (sysArgs.contains(s"--$argName"))

   argValue = GlueArgParser.getResolvedOptions(sysArgs, Array(argName))(argName)


查看完整回答
反对 回复 2021-06-09
  • 3 回答
  • 0 关注
  • 164 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号