简体   繁体   English

Spark:如何覆盖 S3 文件夹上的文件而不是完整的文件夹

[英]Spark: How to overwrite a file on S3 folder and not complete folder

Using Spark I am trying to push some data(in csv, parquet format) to S3 bucket.使用 Spark 我正在尝试将一些数据(以 csv、parquet 格式)推送到 S3 存储桶。

df.write.mode("OVERWRITE").format("com.databricks.spark.csv").options(nullValue=options['nullValue'], header=options['header'], delimiter=options['delimiter'], quote=options['quote'], escape=options['escape']).save(destination_path)

Short answer: Set the Spark configuration parameter spark.sql.sources.partitionOverwriteMode to dynamic instead of static.简短回答:将 Spark 配置参数spark.sql.sources.partitionOverwriteModedynamic而不是静态。 This will only overwrite the necessary partitions and not all of them.这只会覆盖必要的分区,而不是全部。 PySpark example: PySpark 示例:

conf=SparkConf().setAppName("test).set("spark.sql.sources.partitionOverwriteMode","dynamic").setMaster("yarn")
sc = SparkContext(conf=conf)
sqlContext = sql.SQLContext(sc)

The file's can be deleted first and then use append mode to insert the data instead of overwriting to retain the sub folder's.可以先删除文件,然后使用追加模式插入数据,而不是覆盖以保留子文件夹。 Below is an example from Pyspark.以下是 Pyspark 的一个示例。

import subprocess
subprocess.call(["hadoop", "fs", "-rm", "{}*.csv.deflate".format(destination_path)])

df.write.mode("append").format("com.databricks.spark.csv").options(nullValue=options['nullValue'], header=options['header'], delimiter=options['delimiter'], quote=options['quote'], escape=options['escape']).save(destination_path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM