如何在Glue作业中访问AWS Glue工作流的运行属性?

时间:2020-01-02 21:57:02

标签: amazon-web-services aws-cli aws-glue aws-glue-data-catalog

我一直在使用AWS Glue工作流程来编排批处理作业。 我们需要传递push-down-predicate才能限制批处理作业的处理。 当我们单独运行Glue作业时,我们可以在运行时将下推谓词作为命令行参数传递(即aws胶水start-job-run --job-name foo.scala --arguments --arg1-text $ {arg1} ..)。但是,当我们使用胶水工作流程执行胶水作业时,还不清楚。

当我们使用AWS Glue工作流编排批处理作业时,我们可以在创建工作流时添加运行属性。

  1. 我可以使用run属性为我的Glue Job传递下推谓词吗?
  2. 如果是,那么如何在运行时定义run属性的值(下推谓词)。我之所以要在运行时定义下推谓词的值,是因为该谓词每天都会任意更改。 (即,运行胶水工作流过去10天,过去20天,过去2天等)

我尝试过:

aws粘胶start-workflow-run --name工作流名称| jq -r'.RunId'

aws gum put-workflow-run-properties --name工作流名称--run-id“ ID” --run-properties --pushdownpredicate =“某个值”

我能够使用put-workflow-run-property查看我已传递的run属性

aws gum put-workflow-run-properties --name工作流名称--run-id“ ID”

但是我无法在我的胶水作业中检测到“ pushdownpredicate”。 知道如何在Glue Job中访问工作流的run属性吗?

enter image description here

2 个答案:

答案 0 :(得分:1)

如果您将python用作Glue作业的编程语言,则可以发出get_workflow_run_properties API调用以检索该属性并将其在您的Glue作业中使用。

response = client.get_workflow_run_properties(
    Name='string',
    RunId='string'
)

这将为您提供以下响应,您可以对其进行解析和使用:

{
    'RunProperties': {
        'string': 'string'
    }
}

如果您使用的是Scala,则可以使用等效的AWS开发工具包。

答案 1 :(得分:0)

首先,您需要确保作业是从工作流中运行的:

def get_worfklow_params(args: Dict[str, str]) -> Dict[str, str]:
    """
    get_worfklow_params is delegated to retrieve the WORKFLOW parameters
    """
    glue_client = boto3.client("glue")
    if "WORKFLOW_NAME" in args and "WORKFLOW_RUN_ID" in args:
        workflow_args = glue_client.get_workflow_run_properties(Name=args['WORKFLOW_NAME'], RunId=args['WORKFLOW_RUN_ID'])["RunProperties"]
        print("Found the following workflow args: \n{}".format(workflow_args))
        return workflow_args
    print("Unable to find run properties for this workflow!")
    return None

此方法将返回 workflow 输入参数的映射。

您可以使用以下方法来检索给定参数:

def get_worfklow_param(args: Dict[str, str], arg) -> str:
    """
    get_worfklow_param is delegated to verify if the given parameter is present in the job and return it. In case of no presence None will be returned
    """
    if args is None:
        return None
    return args[arg] if arg in args else None

从重用代码来看,我认为最好创建一个python(whl)模块并将该模块设置在您的作业的脚本路径中。通过这种方式,您可以通过简单的导入来检索该方法。

如果没有 whl 模块,您可以按以下方式移动:


def MyTransform(glueContext, dfc) -> DynamicFrameCollection:
    import boto3
    import sys
    from typing import Dict

    def get_worfklow_params(args: Dict[str, str]) -> Dict[str, str]:
    """
    get_worfklow_params is delegated to retrieve the WORKFLOW parameters
    """
    glue_client = boto3.client("glue")
    if "WORKFLOW_NAME" in args and "WORKFLOW_RUN_ID" in args:
        workflow_args = glue_client.get_workflow_run_properties(
            Name=args['WORKFLOW_NAME'], RunId=args['WORKFLOW_RUN_ID'])["RunProperties"]
        print("Found the following workflow args: \n{}".format(workflow_args))
        return workflow_args
    print("Unable to find run properties for this workflow!")
    return None

    def get_worfklow_param(args: Dict[str, str], arg) -> str:
    """
    get_worfklow_param is delegated to verify if the given parameter is present in the job and return it. In case of no presence None will be returned
    """
    if args is None:
        return None
    return args[arg] if arg in args else None

    _args = getResolvedOptions(sys.argv, ['JOB_NAME', 'WORKFLOW_NAME', 'WORKFLOW_RUN_ID'])
    worfklow_params = get_worfklow_params(_args)


    job_run_id = get_worfklow_param(_args, "WORKFLOW_RUN_ID")
    my_parameter= get_worfklow_param(_args, "WORKFLOW_CUSTOM_PARAMETER")