You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Boto3 create_job为AWS Glue Job传递参数

解决Glue Job默认参数传递到数据源路径的问题

你遇到的核心问题是混淆了自定义脚本运行和CodeGen自动生成脚本两种Glue Job创建方式的参数引用逻辑,以下分两种场景给出解决方案:


场景1:使用自定义脚本(推荐方式)

如果你的Job依赖自己编写的gluejob.py脚本(即通过ScriptLocation指定脚本路径),不需要使用CodeGenConfigurationNodes,直接在脚本内部读取默认参数即可:

1. 修正Boto3创建Job的代码

保留DefaultArguments的参数配置,移除多余的CodeGenConfigurationNodes块:

import boto3

client = boto3.client('glue')
response = client.create_job(
    Name='jobname',
    Description='Glue Job',
    LogUri='s3://bucket/logs/',
    Role='arn:aws:iam::123456789012:role/GlueExecutionRole',  # 补充完整的Role ARN
    ExecutionProperty={
        'MaxConcurrentRuns': 3
    },
    Command={
        'Name': 'glueetl',
        'ScriptLocation': 's3://bucketname/gluejob.py',
        'PythonVersion': '3'
    },
    MaxRetries=1,
    Timeout=123,
    GlueVersion='3.0',
    NumberOfWorkers=2,
    WorkerType='G.1X',
    DefaultArguments={'s3sourcepath': 's3://bucketname/csvfile.csv'}
)

2. 在自定义脚本中读取参数

在gluejob.py里添加参数读取逻辑,将参数作为数据源路径:

import sys
from awsglue.utils import getResolvedOptions
from awsglue.context import GlueContext

# 读取默认参数
args = getResolvedOptions(sys.argv, ['s3sourcepath'])
glue_context = GlueContext(None)

# 使用参数作为S3数据源路径
s3_source = glue_context.create_dynamic_frame.from_options(
    connection_type="s3",
    connection_options={"paths": [args['s3sourcepath']]},
    format="csv",
    format_options={
        "separator": ",",
        "quoteChar": "\"",
        "withHeader": True
    }
)

# 后续ETL逻辑...

场景2:使用CodeGen自动生成脚本

如果依赖Glue的CodeGen功能自动生成ETL脚本,直接在CodeGenConfigurationNodes的路径中使用**参数占位符${参数名}**即可,Glue会自动替换为DefaultArguments中的值:

import boto3

client = boto3.client('glue')
response = client.create_job(
    Name='jobname',
    Description='Glue Job',
    LogUri='s3://bucket/logs/',
    Role='arn:aws:iam::123456789012:role/GlueExecutionRole',
    ExecutionProperty={
        'MaxConcurrentRuns': 3
    },
    Command={
        'Name': 'glueetl',
        'PythonVersion': '3'
        # 无需指定ScriptLocation,Glue会自动生成脚本
    },
    MaxRetries=1,
    Timeout=123,
    GlueVersion='3.0',
    NumberOfWorkers=2,
    WorkerType='G.1X',
    DefaultArguments={'s3sourcepath': 's3://bucketname/csvfile.csv'},
    CodeGenConfigurationNodes={
        'node-1': {
            'S3CsvSource': {
                'Name': 's3_source',
                'Paths': [
                    '${s3sourcepath}'  # 使用参数占位符引用默认参数
                ],
                'Separator': ',',
                'QuoteChar': '"',
                'WithHeader': True,
                'WriteHeader': True
            }
        }
    }
)

注意事项

  • 命令名称需使用glueetl而非glue,对应ETL类型的Job
  • 必须补充完整的IAM Role ARN,不能留空
  • 两种方式二选一,不要同时指定ScriptLocation和CodeGenConfigurationNodes

内容的提问来源于stack exchange,提问作者Techie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 01:25:21