Saving empty DataFrame with known schema (Spark 2.2.1)

不羁岁月 提交于 2019-12-10 07:24:37

问题


Is it possible to save an empty DataFrame with a known schema such that the schema is written to the file, even though it has 0 records?

def example(spark: SparkSession, path: String, schema: StructType) = { 
  val dataframe = spark.createDataFrame(spark.sparkContext.emptyRDD[Row], schema) 
  val dataframeWriter = dataframe.write.mode(SaveMode.Overwrite).format("parquet") 
  dataframeWriter.save(path) 

  spark.read.load(path) // ERROR!! No files to read, so schema unknown 
} 

回答1:


This is the answer I received from Databricks Support:

This is actually a known issue in Spark. There is already fix done in opensource JIRA -> https://issues.apache.org/jira/browse/SPARK-23271. For more details on how this behavior will change from 2.4 please check this doc change https://github.com/apache/spark/pull/20525/files#diff-d8aa7a37d17a1227cba38c99f9f22511R1808 The behavior will be changed from Spark 2.4. Until then you need to go with any one of the following ways

  1. Save a dataframe with at-least one record to preserve its schema
  2. Save schema in a JSON file and use later



回答2:


I got a similar problem with Spark 2.1.0. I solved it using repartition before writing.

df.repartition(1).write.parquet("my/path")


来源:https://stackoverflow.com/questions/49821408/saving-empty-dataframe-with-known-schema-spark-2-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!