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))
相关推荐
XUEYUAN521210 分钟前
ASN 自治系统号风控:平台如何通过 IP 所属自治域批量识别代理流量
python·网络协议·http·网络安全·socks5
Zenova EdgeOS33 分钟前
Linux ss 工业边缘网络分析实战
linux·python·边缘计算·工业网关
成为深度学习高手1 小时前
CrossLinear:即插即用的跨相关嵌入,让线性模型也能用好外生变量
人工智能·python·深度学习·数据挖掘
陈年老古董1 小时前
MediaPipe 姿态检测与脸部关键点检测
笔记·python·opencv·学习·dlib
伞伞悦读2 小时前
【第37期】Python JSON 与配置详解:序列化、反序列化、嵌套结构和配置文件
开发语言·python·json
Ticnix2 小时前
42 天 71 次提交之后,我重新看了一遍自己的架构决策
python·agent·全栈
Ticnix2 小时前
我调了三个月 overlap=50,它其实一次都没生效
后端·python·agent
DanCheng-studio2 小时前
毕设项目分享 大数据B站数据分析可视化系统
python·毕业设计·毕设
外收内放2 小时前
数据分析(jupyter与pandas初体验)
python·学习
花间相见2 小时前
【计算基础|网络04】—— HTTP接口实战(下):接口测试、鉴权与跨域排错
java·linux·人工智能·后端·python·计算机网络·postman