这是对在 Hive 中将 Spark 数据框另存为动态分区表的跟进。我试图在答案中使用建议,但无法使其在 Spark 1.6.1 中起作用
我正在尝试从 DataFrame 以编程方式创建分区。以下是相关代码(改编自 Spark 测试):
hc.setConf("hive.metastore.warehouse.dir", "tmp/tests")
// hc.setConf("hive.exec.dynamic.partition", "true")
// hc.setConf("hive.exec.dynamic.partition.mode", "nonstrict")
hc.sql("create database if not exists tmp")
hc.sql("drop table if exists tmp.partitiontest1")
Seq(2012 -> "a").toDF("year", "val")
.write
.partitionBy("year")
.mode(SaveMode.Append)
.saveAsTable("tmp.partitiontest1")
hc.sql("show partitions tmp.partitiontest1").show
完整档案在这里:https://gist.github.com/SashaOv/7c65f03a51c7e8f9c9e018cd42aa4c4a
在文件系统上可以很好地创建分区文件,但是 Hive 抱怨该表未分区:
======================
HIVE FAILURE OUTPUT
======================
SET hive.support.sql11.reserved.keywords=false
SET hive.metastore.warehouse.dir=tmp/tests
OK
OK
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Table tmp.partitiontest1 is not a partitioned table
======================
看起来根本原因是org.apache.spark.sql.hive.HiveMetastoreCatalog.newSparkSQLSpecificMetastoreTable
总是创建具有空分区的表。
对此前进的任何帮助表示赞赏。
**编辑:**还创建了SPARK-14927
1 回答
我找到了一种解决方法:如果您 pre-create 表,则 saveAsTable()不会弄乱它。因此,以下工作原理:
此解决方法在 1.6.1 中有效,但在 1.5.1 中无效