作业已提交并成功运行。但是桶中没有数据。我该如何解决?
df = spark.createDataFrame([["Amy", "lily", 12], ["john", "tom", 34]]).toDF(*["first_name", "last_name", "age"])
df.write.format("parquet").partitionBy("age").option("path", "gs://my_bucket/my_table")
最佳答案
问题中的代码配置了写入操作,但从不触发写入本身。
为了实际触发写操作,您需要调用 save
之一。 Writer
中的函数界面。
例如,以下将完成这项工作:
df.write.format("parquet").partitionBy("age").option("path", "gs://my_bucket/my_table").save()
或者:df.write.format("parquet").partitionBy("age").save("gs://my_bucket/my_table")
甚至:df.write.partitionBy("age").parquet("gs://my_bucket/my_table")
模式详情:
df.write
返回 DataFrameWriter
的实例;这是 API:https://spark.apache.org/docs/2.4.6/api/scala/index.html#org.apache.spark.sql.DataFrameWriterDataFrameWriter
API 在精神上与所有其他 Spark API 一致:它是 懒惰 .除非触发操作,否则不会执行任何操作。为此,
DataFrameWriter
的实例行为类似于构建器模式实现:对 format
的后续调用, option
, mode
等。只配置可能最终执行的写操作。一旦操作被配置,你可以通过调用 save
来触发它。或此实例上的类似方法。同样,
DataFrameWriter
还允许您多次重复使用写入操作(例如,配置一组基本选项,然后调用两次以写入 parquet 和 csv 文件,例如;或写入不同的位置等)。
关于apache-spark - 无法将 spark 数据帧写入 gcs 存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63255237/