gpt4 book ai didi

amazon-web-services - 为什么 AWS Glue 会生成多个 json 文件?

转载 作者:行者123 更新时间:2023-12-04 01:54:32 25 4
gpt4 key购买 nike

我正忙于 POC(使用 AWS Glue)从 RDS AWS Postgresql 表中提取数据,我想生成一个 JSON 文件。

我正在使用以下脚本,但它不断生成多个文件,每个文件有 5 行。如何让它只生成 1 个文件?

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

## @params: [JOB_NAME]
args = getResolvedOptions(sys.argv, ['JOB_NAME'])

sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)

## @type: DataSource
## @args: [database = "temp-crawlerdb-xxxxx", table_name = "taxservice__3fa3bf8633994e1a827498190adbe56a_contingencyrunningtotal", transformation_ctx = "datasource0"]
## @return: datasource0
## @inputs: []
datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "temp-crawlerdb-xxxxx", table_name = "taxservice__3fa3bf8633994e1a827498190adbe56a_contingencyrunningtotal", transformation_ctx = "datasource0")

## @type: ApplyMapping
## @args: [mapping = [("stake", "decimal(18,6)", "stake", "decimal(18,6)"), ("branchid", "long", "branchid", "long"), ("winningstake", "decimal(18,6)", "winningstake", "decimal(18,6)"), ("grossrevenue", "decimal(18,6)", "grossrevenue", "decimal(18,6)"), ("vatrate", "decimal(18,6)", "vatrate", "decimal(18,6)"), ("tmstamp", "timestamp", "tmstamp", "timestamp"), ("usrid", "string", "usrid", "string"), ("contingencyexternalreference", "string", "contingencyexternalreference", "string"), ("winnings", "decimal(18,6)", "winnings", "decimal(18,6)"), ("ggrtaxrate", "decimal(18,6)", "ggrtaxrate", "decimal(18,6)"), ("taxpayable", "decimal(18,6)", "taxpayable", "decimal(18,6)"), ("vatpayable", "decimal(18,6)", "vatpayable", "decimal(18,6)")], transformation_ctx = "applymapping1"]
## @return: applymapping1
## @inputs: [frame = datasource0]

applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [("stake", "decimal(18,6)", "stake", "decimal(18,6)"), ("branchid", "long", "branchid", "long"), ("winningstake", "decimal(18,6)", "winningstake", "decimal(18,6)"), ("grossrevenue", "decimal(18,6)", "grossrevenue", "decimal(18,6)"), ("vatrate", "decimal(18,6)", "vatrate", "decimal(18,6)"), ("tmstamp", "timestamp", "tmstamp", "timestamp"), ("usrid", "string", "usrid", "string"), ("contingencyexternalreference", "string", "contingencyexternalreference", "string"), ("winnings", "decimal(18,6)", "winnings", "decimal(18,6)"), ("ggrtaxrate", "decimal(18,6)", "ggrtaxrate", "decimal(18,6)"), ("taxpayable", "decimal(18,6)", "taxpayable", "decimal(18,6)"), ("vatpayable", "decimal(18,6)", "vatpayable", "decimal(18,6)")], transformation_ctx = "applymapping1")

## @type: DataSink
## @args: [connection_type = "s3", connection_options = {"path": "s3://tax-service-xxxxx"}, format = "json", transformation_ctx = "datasink2"]
## @return: datasink2
## @inputs: [frame = applymapping1]
datasink2 = glueContext.write_dynamic_frame.from_options(frame = applymapping1, connection_type = "s3", connection_options = {"path": "s3://tax-service-xxxxx"}, format = "csv", transformation_ctx = "datasink2")

job.commit()

最佳答案

在应用映射之前,执行以下操作:

from awsglue.dynamicframe import DynamicFrame

# Convert to a dataframe and partition based on "partition_col"
partitioned_dataframe = datasource0.toDF().repartition(1)

# Convert back to a DynamicFrame for further processing.
partitioned_dynamicframe = DynamicFrame.fromDF(partitioned_dataframe, glueContext, "partitioned_df")

关于amazon-web-services - 为什么 AWS Glue 会生成多个 json 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51277545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com