在创建或插入带有Spark分区的Hive表时,不允许操作

时间:2018-06-01 06:59:12

标签: apache-spark hive

我正在使用Spark 2.2,我正在尝试基于数据框创建一个Hive表。

我只能使用:

创建一个包含数据的新Hive表
result.write.mode(SaveMode.Overwrite).saveAsTable("db.resultTable")

当我尝试对分区执行相同操作时:

result.write.mode(SaveMode.Overwrite).partitionBy("year", "month", "day").saveAsTable("db.resultTable")

我总是得到错误:

Exception in thread "main" org.apache.spark.sql.AnalysisException: Operation not allowed: ALTER TABLE RECOVER PARTITIONS only works on table with location provided: `db`.`resultTable`;

注意:除了错误之外,它还创建了一个包含正确列的表。它还创建了分区,表中有一个包含Parquet文件的位置(/user/hive/warehouse/db.db/resultTable/year=2017/month=1/day=1)。但它包含没有数据。

我试着找一些答案,但还没找到。根据这个thread,我做的一切都很好。 (我还设置了hive.exec.dynamic.partition和hive.exec.dynamic.partition.mode)

有人知道我错过了什么或做错了吗?

1 个答案:

答案 0 :(得分:0)

不要将其另存为表,而是将其另存为HDFS目录中的文件。

result.write.mode(SaveMode.Overwrite).partitionBy("year", "month", "day").parquet("/path/to/table")