在pysparkDataframe中读取hive分区orc表时逻辑和物理计划的工作原理

tjjdgumg  于 2021-06-27  发布在  Hive
关注(0)|答案(1)|浏览(261)

我创建了一个sparkDataframe,从hdfs位置读取csv。

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方法将此Dataframe保存为配置单元分区orc表

emp_df.repartition(5, 'emp_id').write.format('orc').partitionBy("emp_id").saveAsTable("UDB.temptable")

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

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路径位置读取此Dataframe,则似乎无法使用分区键列过滤数据:

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>

********************************************************************************

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

de90aj5v

de90aj5v1#

在第二个示例中,分区位置已经包含在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)

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

相关问题