Skip to main content
PYSPARK • LESSON 251

Registering Global and Local Temporary Views

How can we expose a PySpark DataFrame as a queryable SQL table within the current SparkSession or across the entire cluster?

Advanced3 Minutes800 XP
🤔 THE QUESTION

How can we expose a PySpark DataFrame as a queryable SQL table within the current SparkSession or across the entire cluster?

💡 WHAT IS IT?

createOrReplaceTempView() binds a DataFrame to a local session SQL name, while createOrReplaceGlobalTempView() shares it across all application sessions in global_temp.

🎯 WHAT IS IT USED FOR?

Enabling SQL-fluent analysts and BI tools to query programmatic DataFrame transformations directly with ANSI SQL.

💻 EXAMPLE
df.createOrReplaceTempView("v_sales_transactions")
df_high_value = spark.sql("SELECT * FROM v_sales_transactions WHERE amount > 5000")

🎯 Mission Objectives

Practice typing production-grade PySpark code for Registering Global and Local Temporary Views.

  • createOrReplaceTempView()
  • createOrReplaceGlobalTempView()
  • Cross-session SQL catalog access