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))
相关推荐
赴33517 分钟前
逻辑回归算法基础介绍,简单的二分类三分类实例
人工智能·python·机器学习·逻辑回归·sklearn·分类算法
计算机毕设定制辅导-无忧学长1 小时前
InfluxDB 与 Python 框架结合:Django 应用案例(二)
python·django·sqlite
java1234_小锋2 小时前
[免费]基于Python的招聘职位信息推荐系统(猎聘网数据分析与可视化)(Django+requests库)【论文+源码+SQL脚本】
python·数据分析·django·python招聘系统·django招聘
im_AMBER2 小时前
学习日志23 python
python·学习
生信大杂烩2 小时前
基于成像空间转录组技术的肿瘤亚克隆CNV原位推断方法
python·数据分析
IT项目分享2 小时前
Python字典完全指南:从基础到实战(2025版)
开发语言·python·it项目网
这里有鱼汤3 小时前
全网最通俗易懂的趋势判断神器:卡尔曼滤波原来这么实用!
后端·python·程序员
这里有鱼汤3 小时前
年化96%的小市值策略的选股逻辑源码来了
后端·python
都叫我大帅哥3 小时前
当知识图谱遇上RAG:GraphRAG全解指南
python·ai编程
FF-Studio3 小时前
25年电赛C题 发挥部分 YOLOv8方案&数据集
python·深度学习·yolo