pyspark.sql.DataFrame.createOrReplaceTempView#
- DataFrame.createOrReplaceTempView(name)[source]#
Creates or replaces a local temporary view with this
DataFrame
.New in version 2.0.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters
- namestr
Name of the view.
Notes
The lifetime of this temporary table is tied to the
SparkSession
that was used to create thisDataFrame
.Examples
Example 1: Creating a local temporary view named ‘people’.
>>> df = spark.createDataFrame([(2, "Alice"), (5, "Bob")], schema=["age", "name"]) >>> df.createOrReplaceTempView("people")
Example 2: Replacing the local temporary view.
>>> df2 = df.filter(df.age > 3) >>> # Replace the local temporary view with the filtered DataFrame >>> df2.createOrReplaceTempView("people") >>> # Query the temporary view >>> df3 = spark.sql("SELECT * FROM people") >>> # Check if the DataFrames are equal ... assert sorted(df3.collect()) == sorted(df2.collect())
Example 3: Dropping the temporary view.
>>> # Drop the local temporary view ... spark.catalog.dropTempView("people") True