【解决方案】Spark 写入 Hive 出现HiveFileFormat`. It doesn't match the specified format `ParquetFileFormat`

背景:

在 python spark 任务中,调用

df.repartition("a","b","c").write.format("parquet").mode(saveMode).partitionBy("date").saveAsTable("table_name")

出现报错,提示:

org.apache.spark.sql.AnalysisException: The format of the existing table arcsoft_analysis.zz_table is `HiveFileFormat`. It doesn't match the specified format `ParquetFileFormat`.

提示我们写入 hive 的 fileformat 不对。

这个报错在 spark 1.x 版本不会报错,在 spark 2.x 版本开始出现。

解决方案

  1. 如果我们的 hive 表不是分区表。将format("parquet")改成 format("hive")即可。

将format设置为Hive以后,无论hive建表的时候,使用的fileformat使用的是哪一种,都没有关系。

  1. 如果我们的 hive 表是分区表,可以先将数据存到一个临时表,再调用 spark.sql 插入数据。
        df = spark.createDataFrame(data, ["a", "b", "c", "ctime"])
        df.createOrReplaceTempView("temp_view")
        spark.sql("""
        INSERT INTO $hive_table_name PARTITION (date = '{}')
        select * from temp_view
        """.format(date))
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容