【Hive|Spark】spark写入hive表存储格式问题

一、问题描述

二、原因分析

1、报错信息分析

2、思考

3、结论

三、解决办法

一、问题描述

spark经过转化的DF/DS,存储hive的一般写法为:

DF.write.format("orc").mode(SaveMode.Append).saveAsTable("default.student")

1、如果hive本身不存在此表,则会在hive自动创建对应的表进行数据存储。

2、如果hive中存在此表,则就会分为两种情况进行考虑。

第一种情况:存在的student表是使用spark写入hive程序自动创建得到的,则这种情况下可以正常写入。

DF.write.format(“orc”).mode(SaveMode.Append).saveAsTable(“default.student”)

第二种情况:存在的student表是使用hive命令创建得到的,这种情况下会报错。

create table student(name string,sex string) stored as orc;

报错信息为:

The format of the existing table default.student is HiveFileFormat. It doesn’t match the specified format OrcFileFormat.;

二、原因分析

  • The format of the existing table default.student is HiveFileFormat是说spark程序认为hive中使用命令创建的orc表,在它的眼中存储格式是HiveFileFormat。
  • It doesn’t match the specified format OrcFileFormat 是说spark程序中要存储的student的dataFram是OrcFileFormat格式,与hive中创建表的存储格式不匹配。

为什么明明在hive中使用命令创建的student表就是orc格式,却和spark程序中要存储指定的orc格式不匹配呢?

  • 在hive中使用命令创建的任何存储格式的表,在spark程序看来都是HiveFileFormat格式的表。
  • spark程序只认自己通过代码向hive创建各种存储格式的表并能与之对应匹配和存储数据。
  • 对于使用hive命令创建过的表,spark对应匹配的指定格式统一为HiveFileFormat格式。

三、解决办法

对于hive命令已经创建过的任何存储格式的表,spark写入的时候统一使用如下写法:

DF.write.format(“Hive”).mode(SaveMode.Append).saveAsTable(“default.student”)

Original: https://blog.csdn.net/hyj_king/article/details/127324334
Author: 郝少
Title: 【Hive|Spark】spark写入hive表存储格式问题

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/817043/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球