pyspark.sql.DataFrame.createTempView#
- DataFrame.createTempView(name)[source]#
Creates a local temporary view with this
DataFrame.The lifetime of this temporary table is tied to the
SparkSessionthat was used to create thisDataFrame. throwsTempTableAlreadyExistsException, if the view name already exists in the catalog.New in version 2.0.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters
- namestr
Name of the view.
Examples
Example 1: Creating and querying a local temporary view
>>> 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| +---+-----+
Example 2: Attempting to create a temporary view with an existing name
>>> df.createTempView("people") Traceback (most recent call last): ... AnalysisException: "Temporary table 'people' already exists;"
Example 3: Creating and dropping a local temporary view
>>> spark.catalog.dropTempView("people") True >>> df.createTempView("people")
Example 4: Creating temporary views with multiple DataFrames with
SparkSession.table()>>> 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| +---+----+