在 pyspark 数据帧中读取 Hive Partitioned ORC table 时逻辑和物理计划如何工作

How Logical and Physical plan works when read Hive Partitioned ORC table in pyspark dataframe

我创建了一个从 hdfs 位置读取 csv 的 spark 数据框。

emp_df = spark.read.format("com.databricks.spark.csv") \
  .option("mode", "DROPMALFORMED") \
  .option("header", "true") \
  .option("inferschema", "true") \
  .option("delimiter", ",").load(PATH_TO_FILE)

并使用 partitionBy 方法

将此数据帧保存为 Hive 分区的 orc table
emp_df.repartition(5, 'emp_id').write.format('orc').partitionBy("emp_id").saveAsTable("UDB.temptable")

当我按照下面的方法阅读此 table 时,如果我查看逻辑和物理计划,它似乎已经使用分区键列完美地过滤了数据:

emp_df_1 = spark.sql("select * from UDB.temptable where emp_id ='6'")
emp_df_1.explain(True)

***************************************************************************
== Parsed Logical Plan ==
'Project [*]
+- 'Filter ('emp_id = 6)
   +- 'UnresolvedRelation `UDB`.`temptable`

== Analyzed Logical Plan ==
emp_name: string, emp_city: string, emp_salary: int, emp_id: int
Project [emp_name#7399, emp_city#7400, emp_salary#7401, emp_id#7402]
+- Filter (emp_id#7402 = cast(6 as int))
   +- SubqueryAlias temptable
      +- Relation[emp_name#7399,emp_city#7400,emp_salary#7401,emp_id#7402] orc

== Optimized Logical Plan ==
Filter (isnotnull(emp_id#7402) && (emp_id#7402 = 6))
+- Relation[emp_name#7399,emp_city#7400,emp_salary#7401,emp_id#7402] orc

== Physical Plan ==
*(1) FileScan orc udb.temptable[emp_name#7399,emp_city#7400,emp_salary#7401,emp_id#7402] Batched: true, Format: ORC, Location: PrunedInMemoryFileIndex[hdfs://pathlocation/database/udb...., 
PartitionCount: 1, PartitionFilters: [isnotnull(emp_id#7402), (emp_id#7402 = 6)], PushedFilters: [], ReadSchema: struct<emp_name:string,emp_city:string,emp_salary:int>
***************************************************************************

而如果我通过绝对 hdfs 路径位置读取此数据帧,似乎无法使用分区键列过滤数据:

emp_df_2 = spark.read.format("orc").load("hdfs://pathlocation/database/udb.db/temptable/emp_id=6")
emp_df_2.explain(True)

******************************************************************************
== Parsed Logical Plan ==
Relation[emp_name#7411,emp_city#7412,emp_salary#7413] orc

== Analyzed Logical Plan ==
emp_name: string, emp_city: string, emp_salary: int
Relation[emp_name#7411,emp_city#7412,emp_salary#7413] orc

== Optimized Logical Plan ==
Relation[emp_name#7411,emp_city#7412,emp_salary#7413] orc

== Physical Plan ==
*(1) FileScan orc [emp_name#7411,emp_city#7412,emp_salary#7413] Batched: true, Format: ORC, Location: InMemoryFileIndex[hdfs://pathlocation/data/database/udb.db/tem..., 
PartitionFilters: [], PushedFilters: [], ReadSchema: struct<emp_name:string,emp_city:string,emp_salary:int>
********************************************************************************

你能帮我理解这两种情况下的逻辑和物理计划吗?

在您的第二个示例中,分区位置已包含在 HDFS 路径中。您仍然可以将父目录作为路径并使用以下代码进行分区:

full_dataset_df = spark.read.format("orc") \
    .load("hdfs://pathlocation/database/udb.db/temptable")
one_partition_df = full_dataset_df.where(full_dataset_df.emp_id == 6)

值得一提的是,无论使用这三种方法中的哪一种,数据处理性能都是一样的。