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?

Advancedโฑ 3 Minutesโญ 800 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