运行 Sagemaker 管道的ProcessingStep 时如何将参数传递到我的Python 文件?

问题描述 投票:0回答:1

我从这个文档中读到,ProcessingStep可以接受作业参数。

我目前有一个Python脚本,其中包含一个要通过ProcessingStep执行的函数,该函数需要解析参数。我不知道如何从“作业参数”中提取参数,以便我可以在Python脚本中调用该函数与论据。

这是我的 python 脚本中的代码片段示例:

def params(input_params):
    details = {"database": input_params[0],
         "table": input_params[1], 
         "catalog": input_params[2], 
         "earliestday": int(input_params[3]), 
         "latestday": int(input_params[4]),
         "s3bucket": input_params[5], 
         "bucketpath": input_params[6]}
    return details

output_params = params(input_params) #this is where I'm not sure how I can extract the argument from the job arguments in the ProcessingStep to call my function here

这是我的处理步骤代码的样子:

step_params = ProcessingStep(
    name="StateParams",
    processor=sklearn_processor, 
    outputs = [processing_output],
    job_arguments = ["ABC", "SESSION_123", "AwsDataCatalog", "5", "7", "mybucket", "bucket2/tmp/athena_sagemaker"],   #This is the job argument I input which I hope will be parsed into my python file function
    code = "params.py",
)

如果你们中有人能建议我如何使用ProcessingStep中的作业参数来成功调用python脚本中的函数,我将不胜感激!

amazon-web-services amazon-sagemaker
1个回答
1
投票

您可以将作业参数传递给ProcessingStep,如下所示:

sample_argument_1 = "sample_arg_1"
sample_argument_2 = "sample_arg_2"

step_params = ProcessingStep(
    name="StateParams",
    processor=sklearn_processor, 
    outputs = [processing_output],
    job_arguments = ["--sample-argument-1", sample_argument_1,  "--sample-argument-2", sample_argument_2], 
    code = "params.py",
)

在 params.py 文件中,您可以使用如下参数:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--sample-argument-1', type=str, dest='sample_argument_1')
parser.add_argument('--sample-argument-2', type=str, dest='sample_argument_2')
    
args = parser.parse_args()
arg_1 = args.sample_argument_1
print("arg_1:")
print(arg_1)
© www.soinside.com 2019 - 2024. All rights reserved.