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))
相关推荐
无妄无望1 小时前
ragflow代码学习切片方式(1)docling_parser.py
人工智能·python·学习
醒过来摸鱼1 小时前
9.12 sinc插值
python·线性代数·算法·numpy
小兔崽子去哪了1 小时前
Numpy、Panads
python·numpy·pandas
dagouaofei2 小时前
开题报告自动做PPT
python·powerpoint
Hello 0 13 小时前
视频号直播视频录制
python·音视频·流媒体·直播视频录制
FreeCode3 小时前
LangSmith本地部署LangGraph应用
python·langchain·agent
mit6.8243 小时前
py期中实验选题:实现天气预测
python·算法
2501_941089193 小时前
5G技术与物联网的融合:智能城市与工业革命的加速器
spark
Rolei_zl4 小时前
AIGC(生成式AI)试用 41 -- 程序(Python + OCR)-3
python·aigc