AWS EMR Spark:写入 S3 时出错 - IllegalArgumentException - 无法从空字符串创建路径

AWS EMR Spark: Error writing to S3 - IllegalArgumentException - Cannot create a path from an empty string

我已经尝试解决这个问题很长时间了……不知道为什么我会得到这个?仅供参考,我在 AWS EMR 集群上的集群上 运行 Spark。我调试并清楚地看到提供的目标路径......类似于 s3://my-bucket-name/。 spark 作业创建 orc 文件并在创建分区后写入它们,如下所示:date=2017-06-10。有什么想法吗?

17/07/08 22:48:31 ERROR ApplicationMaster: User class threw exception: java.lang.IllegalArgumentException: Can not create a Path from an empty string
java.lang.IllegalArgumentException: Can not create a Path from an empty string
    at org.apache.hadoop.fs.Path.checkPathArg(Path.java:126)
    at org.apache.hadoop.fs.Path.<init>(Path.java:134)
    at org.apache.hadoop.fs.Path.<init>(Path.java:93)
    at org.apache.hadoop.fs.Path.suffix(Path.java:361)
    at org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelationCommand.deleteMatchingPartitions(InsertIntoHadoopFsRelationCommand.scala:138)
    at org.apache.spark.sql.execution.datasources.InsertIntoHadoopFsRelationCommand.run(InsertIntoHadoopFsRelationCommand.scala:82)

写orc的代码:

dataframe.write
   .partitionBy(partition)
   .option("compression", ZLIB.toString)
   .mode(SaveMode.Overwrite)
   .orc(destination)

我在将 parquet 文件写入 S3 时看到了类似的问题。问题是SaveMode.Overwrite。这种模式似乎不能与 S3 一起正常工作。在写入之前尝试删除 S3 存储桶 my-bucket-name 中的所有数据。那么你的代码应该 运行 成功。

要删除存储桶中的所有文件 my-bucket-name,您可以使用以下 pyspark 代码:

# see https://www.quora.com/How-do-you-overwrite-the-output-directory-when-using-PySpark
URI = sc._gateway.jvm.java.net.URI
Path = sc._gateway.jvm.org.apache.hadoop.fs.Path
FileSystem = sc._gateway.jvm.org.apache.hadoop.fs.FileSystem

# see http://crazyslate.com/how-to-rename-hadoop-files-using-wildcards-while-patterns/
fs = FileSystem.get(URI("s3a://my-bucket-name"), sc._jsc.hadoopConfiguration())
file_status = fs.globStatus(Path("/*"))
for status in file_status:
    fs.delete(status.getPath(), True)