问题描述:
在使用AWS Glue write_dynamic frame将数据写入S3时,有时候会发现某些记录中会自动添加双引号,导致数据格式不符合预期,造成后续处理时的问题。
这个问题的原因是AWS Glue在将数据写入S3时,会自动将数据进行转义处理。而在某些特定情况下,这种转义处理会导致数据的格式不符合预期。
为了避免这个问题,我们可以在代码中指定write_dynamic frame的输出格式。具体代码如下:
# 导入动态帧库
from awsglue.dynamicframe import DynamicFrame
# 定义一个动态帧
dynamic_frame = DynamicFrame.from_options(frame_or_dfs=someDataFrame,
connection_type="s3",
format="csv",
format_options={"quoteChar":'"', "escapeChar":''})
# 写动态帧到S3
dynamic_frame.toDF().write.format("csv").option("quote", "").save("s3://some-bucket/some-prefix/")
在上面的代码中,我们通过指定输出格式为csv,并添加format_options参数,明确指定quoteChar为双引号,escapeChar为空,来避免自动转义处理,从而解决了数据格式不符合预期的问题。
参考链接:
https://stackoverflow.com/questions/59688096/aws-glue-write-dynamic-frame-is-automatically-adding-double-quotes-to-some-recor