Spark 写Hive出现HiveFileFormat. It doesn't match the specified format 错误

背景:

在 python spark 任务中,调用

lua 复制代码
df.repartition("a","b","c").write.format("parquet").mode(saveMode).partitionBy("date").saveAsTable("table_name")

出现报错,提示:

swift 复制代码
org.apache.spark.sql.AnalysisException: The format of the existing table xxx 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 插入数据。
python 复制代码
        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))
相关推荐
悻运几秒前
如何配置Spark
大数据·分布式·spark
懒惰的橘猫11 分钟前
Spark集群搭建之Yarn模式
大数据·分布式·spark
偶尔微微一笑23 分钟前
AI网络渗透kali应用(gptshell)
linux·人工智能·python·自然语言处理·编辑器
2401_8242568626 分钟前
Spark-Streaming
大数据·分布式·spark
元6331 小时前
spark和hadoop之间的对比和联系
大数据·hadoop·spark
船长@Quant2 小时前
文档构建:Sphinx全面使用指南 — 基础篇
python·markdown·sphinx·文档构建
喵手2 小时前
从 Java 到 Kotlin:在现有项目中迁移的最佳实践!
java·python·kotlin
liuweidong08022 小时前
【Pandas】pandas DataFrame rsub
开发语言·python·pandas
沐风¥2 小时前
Kafka和Spark-Streaming
spark
CH3_CH2_CHO3 小时前
不吃【Numpy】版
开发语言·python·numpy