AWS Glue- 确保 S3 存储桶中只有一个输出文件
创始人
2024-11-16 06:00:16
0

要确保AWS Glue将数据仅写入S3存储桶中的一个输出文件,可以使用以下代码示例:

import boto3

def create_glue_job(job_name, script_location, s3_output_path):
    glue_client = boto3.client('glue')

    response = glue_client.create_job(
        Name=job_name,
        Role='Glue_DefaultRole', # Glue作业所需的IAM角色
        Command={
            'Name': 'glueetl',
            'ScriptLocation': script_location
        },
        DefaultArguments={
            '--output_path': s3_output_path
        },
        GlueVersion='2.0',
        MaxRetries=0,
        NumberOfWorkers=2,
        Timeout=60
    )

    return response['JobName']

def main():
    job_name = 'example-glue-job'
    script_location = 's3://example-bucket/glue_scripts/glue_job.py'
    s3_output_path = 's3://example-bucket/output/output_file.json'

    create_glue_job(job_name, script_location, s3_output_path)

if __name__ == '__main__':
    main()

上述代码示例使用AWS SDK for Python(即boto3库)创建了一个AWS Glue作业。在创建作业时,通过DefaultArguments参数将S3输出路径传递给Glue脚本。

在Glue脚本中,您可以使用以下代码逻辑确保仅输出一个文件:

import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job

def main():
    glueContext = GlueContext(SparkContext.getOrCreate())
    spark = glueContext.spark_session

    # 获取作业参数
    args = getResolvedOptions(sys.argv, ['JOB_NAME', 'output_path'])

    # 读取数据
    data_frame = glueContext.create_dynamic_frame.from_catalog(database = "database_name", table_name = "table_name")

    # 处理数据
    # ...

    # 写入输出文件
    data_frame.toDF().repartition(1).write.format("json").mode("overwrite").save(args['output_path'])

if __name__ == '__main__':
    main()

在上述脚本中,我们使用repartition(1)将数据帧重新分区为一个分区(一个输出文件)。然后使用write.format("json").mode("overwrite").save(args['output_path'])将数据帧写入S3输出路径。

请确保将database_nametable_name替换为实际的数据库名称和表名称。

这样,AWS Glue作业将确保仅输出一个文件到S3存储桶中。

相关内容

热门资讯

盘点一款!WepOke(WeP... WePoker透视辅助版本稳定性对比与推荐‌:盘点一款!WepOke(WePoKer)外挂透明挂辅助...
玩家必看分享(pokerrrr... 玩家必看分享(pokerrrr2)外挂透明挂辅助神器(辅助挂)发牌规律(揭秘有挂)-哔哩哔哩准备好在...
透视了解(WPK漏洞)外挂透明... 透视了解(WPK漏洞)外挂透明挂辅助器安装(透视)软件透明挂(真的有挂)-哔哩哔哩这是由厦门游乐互动...
免费测试版(wpk打法)外挂透... 免费测试版(wpk打法)外挂透明挂辅助工具(辅助挂)其实是真的有挂(2021已更新)(哔哩哔哩);(...
透视最新!WPK存在(wepo... 透视最新!WPK存在(wepoKe)外挂透明挂辅助挂(辅助挂)详细教程(确实有挂)-哔哩哔哩准备好在...
六分钟了解(wpk德州俱乐部)... 六分钟了解(wpk德州俱乐部)外挂透明挂辅助机制(透视)德州ai机器人(新版有挂)-哔哩哔哩;wpk...
查到实测辅助!来玩德州app(... 查到实测辅助!来玩德州app(WEPoke)外挂透明挂辅助插件(辅助挂)攻略教程(有挂分析)-哔哩哔...
重大通报(WPK开挂)外挂透明... 1、重大通报(WPK开挂)外挂透明挂辅助插件(辅助挂)透视辅助(有挂解密)-哔哩哔哩。2、WPK开挂...
透明安装(德扑之星底牌)外挂透... 透明安装(德扑之星底牌)外挂透明挂辅助神器(透视)的确是真的有挂(2022已更新)(哔哩哔哩);科技...
我来教大家!pokerrrr2... 《我来教大家!pokerrrr2俱乐部(wEpOke)外挂透明挂辅助插件(辅助挂)第三方教程(有挂教...