gpt4 book ai didi

apache-spark - 如何在 Spark 中获取 hive 表的分区信息

转载 作者:可可西里 更新时间:2023-11-01 15:19:29 24 4
gpt4 key购买 nike

我想像这样通过 Spark 执行 SQL。

sparkSession.sql("select * from table")

但我想在执行前对表进行分区检查,避免全扫描。

如果表是分区表,我的程序会强制用户添加分区过滤器。如果没有,可以运行。

所以我的问题是如何知道一个表是否是分区表?我的想法是从 Metastore 读取信息。但是如何获取 Metastore 是我遇到的另一个问题。有人可以帮忙吗?

最佳答案

假设您的真正目标是限制无界查询的执行,我认为获取查询的执行计划并查看其 FileScan/HiveTableScan 叶节点会更容易查看是否正在应用任何分区过滤器。顺便说一句,对于分区表,还会显示查询实际要扫描的分区数。所以,应该这样做:

scala> val df_unbound = spark.sql("select * from hottab")
df_unbound: org.apache.spark.sql.DataFrame = [id: int, descr: string ... 1 more field]

scala> val plan1 = df_unbound.queryExecution.executedPlan.toString
plan1: String =
"*(1) FileScan parquet default.hottab[id#0,descr#1,loaddate#2] Batched: true, Format: Parquet,
Location: CatalogFileIndex[hdfs://ns1/user/hive/warehouse/hottab],
PartitionCount: 365, PartitionFilters: [],
PushedFilters: [], ReadSchema: struct<id:int,descr:string>
"

scala> val df_filtered = spark.sql("select * from hottab where loaddate='2019-07-31'")
df_filtered: org.apache.spark.sql.DataFrame = [id: int, descr: string ... 1 more field]

scala> val plan2 = df_filtered.queryExecution.executedPlan.toString
plan2: String =
"*(1) FileScan parquet default.hottab[id#17,descr#18,loaddate#19] Batched: true, Format: Parquet,
Location: PrunedInMemoryFileIndex[hdfs://ns1/user/hive/warehouse/hottab/loaddate=2019-07-31],
PartitionCount: 1, PartitionFilters: [isnotnull(loaddate#19), (loaddate#19 = 2019-07-31)],
PushedFilters: [], ReadSchema: struct<id:int,descr:string>
"

这样,您也不必处理 SQL 解析以从查询中查找表名,也不必自己查询 Metastore。

作为奖励,除了分区修剪之外,您还可以查看是否发生“常规”过滤器下推(对于支持它的存储格式)。

关于apache-spark - 如何在 Spark 中获取 hive 表的分区信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57290634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com