前言
Spark操作Hive表可谓是异常的方便和简单,这里根据官网简单的总结一下Spark操作Hive分区表
完美的处理处理方式
// 开启Hive动态分区
spark.sqlContext.setConf("hive.exec.dynamic.partition", "true")
spark.sqlContext.setConf("hive.exec.dynamic.partition.mode", "nonstrict")
// 使用Spark DataFrame API创建Hive分区表,并把DataFrame的数据入库
df.write.partitionBy("key").format("hive").saveAsTable("hive_part_tbl")
说明:
为什么说是完美的处理方式:
官网给的不会有bug
如果hive中不存在该表,则spark会自动创建此分区表,并将数据入库,如果先创建好了hive表,第一次执行上代码,spark直接将数据入库,之后再执行,当然就会报错:org.apache.spark.sql.AnalysisException: Table hive_part_tb1 already exists.;
此时需要根据业务需求调整mode参数:
/**
* Specifies the behavior when data or table already exists. Options include:
* - `overwrite`: overwrite the existing data.
* - `append`: append the data.
* - `ignore`: ignore the operation (i.e. no-op).
* - `error` or `errorifexists`: default option, throw an exception at runtime.
*
* @since 1.4.0
*/
比如要追加则:
df.write.mode("append").partitionBy("key").format("hive").saveAsTable("hive_part_tbl")
而如果不写调用mode方法,默认则是error
or errorifexists
,因此上面除了第一次执行,后面都会报表已存在的错误咯
不完美的处理方式
- 方式一:
df.write.mode("append").partitionBy("key").saveAsTable("hive_part_tb2")
分析:
如果Hive中不存在该表(hive_part_tb2),那么spark会创建该表,并将数据入库,此时仍然完美处理,但是
如果提前创建了Hive表,执行上述代码就会报错:
Exception in thread "main" org.apache.spark.sql.AnalysisException:
The format of the existing table hive_part_tb2 is `HiveFileFormat`.
It doesn't match the specified format `ParquetFileFormat`.;
报错原因:
Spark默认的文件格式为PARQUET,为在命令行Hive默认的文件格式为TEXTFILE
方式二:
// 开启Hive动态分区
spark.sqlContext.setConf("hive.exec.dynamic.partition", "true")
spark.sqlContext.setConf("hive.exec.dynamic.partition.mode", "nonstrict")
df.createOrReplaceTempView("view1")
sql("insert into hive_part_tb3 select * from view1")
或者
// 开启Hive动态分区
spark.sqlContext.setConf("hive.exec.dynamic.partition", "true")
spark.sqlContext.setConf("hive.exec.dynamic.partition.mode", "nonstrict")
df.write.insertInto("hive_part_tb3")
说明:
这两种方式可以归类为一种,都不需要指定分区字段,如果有一个分区,那么默认为数据中最后一列为分区字段,有两个分区则为最后两列为分区字段,以此类推
当然,如果目标Hive表未创建,则同样会报错:
org.apache.spark.sql.AnalysisException: Table or view not found: hive_part_tb3;
这种方式,感觉几乎和推荐的方式一致了,但是个人感觉可读性不强
后记
上面三种方式都可以实现功能,但是第一种是推荐的方式