spark写入hive表

一、问题描述
        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`.;

二、原因分析
1、报错信息分析
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中创建表的存储格式不匹配。
2、思考
        为什么明明在hive中使用命令创建的student表就是orc格式,却和spark程序中要存储指定的orc格式不匹配呢?

3、结论
在hive中使用命令创建的任何存储格式的表,在spark程序看来都是HiveFileFormat格式的表。
spark程序只认自己通过代码向hive创建各种存储格式的表并能与之对应匹配和存储数据。
对于使用hive命令创建过的表,spark对应匹配的指定格式统一为HiveFileFormat格式。
三、解决办法
        对于hive命令已经创建过的任何存储格式的表,spark写入的时候统一使用如下写法:

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


 

【Hive|Spark】spark写入hive表存储格式问题_spark将数据存入hive_郝少的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/eylier/article/details/130505534