apache-spark - 获取 Spark 写入 Hive Metastore 的所有新分区
问题描述
我有一个数据框,我使用 spark sql(使用动态分区)将其插入到现有的分区配置单元表中。写入数据框后,我想知道我的数据框刚刚在 hive 中创建的分区是什么。
我可以查询不同分区的数据帧,但它需要很长时间,因为它必须启动数据帧的整个沿袭。
我可以在写入配置单元之前保留数据帧,这样,写入操作和不同的 partition_column 操作发生在缓存的数据帧之上。但是我的数据框非常大,不想花更多的时间来坚持。
我知道所有分区信息都存储在 Hive Metastore 中。spark中是否有任何metastore api可以帮助仅检索创建的新分区?
解决方案
您可以使用HiveMetastoreClient检索表的分区数据:
import org.apache.hadoop.hive.conf.HiveConf
import scala.collection.JavaConverters._
import org.apache.hadoop.hive.metastore.HiveMetaStoreClient
val hiveConf = new HiveConf(spark.sparkContext.hadoopConfiguration, classOf[HiveConf])
val cli = new HiveMetaStoreClient(hiveConf)
/* Get list of partition values prior to DF insert */
val existingPartitions = cli.listPartitions("<db_name>", "<tbl_name>", Short.MaxValue).asScala.map(_.getValues.asScala.mkString(","))
/* Insert DF contents to table */
df.write.insertInto("<db_name>.<tbl_name>")
/* Fetch list of partition values again, and diff with previous list */
val newPartitions = cli.listPartitions("<db_name>", "<tbl_name>", Short.MaxValue).asScala.map(_.getValues.asScala.mkString(","))
val deltaPartitions = newPartitions.diff(existingPartitions)
推荐阅读
- sql - 查询以查找给定值落在 MS SQL Server 中的哪个范围
- javascript - 使用 React 钩子更新动态状态的方法是什么?
- javascript - 从 DB 获取返回 [object Object] 而不是 Vue JS 中的值
- java - 如何在 JAVA 中使用二维数组从用户那里获取多个输入?
- ios - **[Assert] UIKit 客户端错误:-[UIWindow setScreen:] 如果客户端采用 UIScene 生命周期,则不应调用
- javascript - 在自动完成中未找到结果时显示消息
- ios - dyld:库未加载:@rpath/MapboxMobileEvents.framework/MapboxMobileEvents
- linux - Liberty Profile - 在 Linux 中出现错误 [无法启动新的 UOW。LocalTransactionContainment 已经在工作中处于活动状态]
- java - Spring Boot - 发送短信的调度程序
- java - 管理字符串的有效方法