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))
相关推荐
2301_8067093814 小时前
国内知名的生活水箱制造厂有哪些
python·生活
学掌门14 小时前
超级菜鸟怎么学习数据分析?
python·学习·数据分析
鸿芯微控科技14 小时前
压电纳米定位分辨率怎么测?噪声、带宽、最小可检测位移与Python分析
开发语言·python·噪声分析·压电执行器·纳米定位·位移测量
用户77833661321114 小时前
从 0 搭一个关键词排名监控:核心思路 + 可运行代码
后端·python·api
Fanta丶14 小时前
14.Python闭包、装饰器
python
码云骑士14 小时前
99-混合搜索Hybrid-Search-BM25-稀疏稠密向量-融合排序
python
金銀銅鐵15 小时前
[Python] 借助 Turtle 逐字展示《醉翁亭记》
python
用户36105886261215 小时前
Spark 核心之广播变量、累加器原理深度剖析
大数据·spark
用户70887690393815 小时前
给传统 SaaS 增加 AI 能力:3个真实落地场景
python