AWSGlueETL作业从文件中删除分区键
创始人
2024-09-25 15:01:47
0

在AWS Glue ETL作业中,可以使用以下代码示例来删除文件中的分区键:

  1. 从数据目录中获取文件列表。
  2. 对于每个文件,使用 AWS Glue的DynamicFrame API读取数据。
  3. 在DynamicFrame中删除分区键。
  4. 将修改的DynamicFrame写回到输出目录。

以下是代码示例:

import boto3
import sys
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.dynamicframe import DynamicFrame

glueContext = GlueContext(SparkContext.getOrCreate())
spark = glueContext.spark_session
s3 = boto3.client('s3', region_name='us-east-1')

args = getResolvedOptions(sys.argv, ['JOB_NAME', 'input_bucket', 'input_prefix', 'output_bucket', 'output_prefix'])
input_bucket = args['input_bucket']
input_prefix = args['input_prefix']
output_bucket = args['output_bucket']
output_prefix = args['output_prefix']

# Get list of files in input S3 bucket and prefix
response = s3.list_objects_v2(Bucket=input_bucket, Prefix=input_prefix)
files = []

for obj in response['Contents']:
    files.append(obj['Key'])

for file in files:
    datasource0 = glueContext.create_dynamic_frame.from_options(
        connection_type="s3",
        connection_options={"paths": [f"s3://{input_bucket}/{file}"]},
        format="csv",
        format_options={"header": True}
    )

    # Removing partition keys from DynamicFrame
    data_without_partition_keys = datasource0.drop_fields(
        ['partitionColumn1', 'partitionColumn2'])

    # Writing DynamicFrame back to S3 output bucket and prefix
    glueContext.write_dynamic_frame.from_options(
        frame=data_without_partition_keys,
        connection_type="s3",
        connection_options={"path": f"s3://{output_bucket}/{output_prefix}/{file}"},
        format="csv",
        format_options={"header": True}
    )

print("ETL job finished")

此作业将从输入S3路径读取每个文件,并删除DynamicFrame中的分区键。接下来,作业将修改的DynamicFrame写回到输出S3路径。

相关内容

热门资讯

一分钟辅助!hhpoker有后... 一分钟辅助!hhpoker有后台操作吗,wepoker辅助器有哪些功能,绝活儿教程(今日头条)1、玩...
5分钟辅助!wepoker透视... 5分钟辅助!wepoker透视挂底牌,wpk安卓下载辅助,诀窍教程(有挂辅助)1、wepoker透视...
第1分钟辅助!wepoker辅... 第1分钟辅助!wepoker辅助器安装包,佛手大菠萝辅助,机巧教程(有挂分享)1、每一步都需要思考,...
第七分钟辅助!wepoker好... 第七分钟辅助!wepoker好友助力码,德普之星辅助器app,积累教程(有挂猫腻)1、德普之星辅助器...
第7分钟辅助!wepoker私... 第7分钟辅助!wepoker私人局辅助器怎么用,约局吧开挂神器是真的吗,模板教程(的确有挂)进入游戏...
3分钟辅助!poker wor... 3分钟辅助!poker world辅助器,竞技联盟透视插件,要领教程(有挂详细)1、首先打开竞技联盟...
第一分钟辅助!pokemmo手... 第一分钟辅助!pokemmo手机脚本辅助器,aa poker辅助,手段教程(的确有挂)运pokemm...
1分钟辅助!wepoker钻石... 1分钟辅助!wepoker钻石怎么看底牌,hhpoker脚本下载,教材教程(有挂解惑)1、上手简单,...
第4分钟辅助!约局吧app有挂... 第4分钟辅助!约局吧app有挂吗,hhpoker作弊码怎么用,方案教程(有挂方针)1、首先打开hhp...
第5分钟辅助!德普之星怎么设置... 第5分钟辅助!德普之星怎么设置埋牌,拱趴大菠萝万能挂图解,积累教程(有挂工具)1、许多玩家不知道德普...