Skip to main content

createTempView

Creates a local temporary view with this DataFrame.

Syntax​

createTempView(name: str)

Parameters​

Parameter

Type

Description

name

str

Name of the view.

Parameter

Type

Description

name

str

Name of the view.

Notes​

The lifetime of this temporary table is tied to the SparkSession that was used to create this DataFrame. throws TempTableAlreadyExistsException, if the view name already exists in the catalog.

Examples​

Python
df = spark.createDataFrame([(2, "Alice"), (5, "Bob")], schema=["age", "name"])
df.createTempView("people")
spark.sql("SELECT * FROM people").show()
# +---+-----+
# |age| name|
# +---+-----+
# | 2|Alice|
# | 5| Bob|
# +---+-----+

df.createTempView("people") # doctest: +IGNORE_EXCEPTION_DETAIL
# Traceback (most recent call last):
# ...
# AnalysisException: "Temporary table 'people' already exists;"

spark.catalog.dropTempView("people")
# True
df.createTempView("people")

df1 = spark.createDataFrame([(1, "John"), (2, "Jane")], schema=["id", "name"])
df2 = spark.createDataFrame([(3, "Jake"), (4, "Jill")], schema=["id", "name"])
df1.createTempView("table1")
df2.createTempView("table2")
result_df = spark.table("table1").union(spark.table("table2"))
result_df.show()
# +---+----+
# | id|name|
# +---+----+
# | 1|John|
# | 2|Jane|
# | 3|Jake|
# | 4|Jill|
# +---+----+