Del via


createTable

Creates a table based on the dataset in a data source.

Syntax

createTable(tableName: str, path: str = None, source: str = None, schema: StructType = None, description: str = None, **options: str)

Parameters

Parameter Type Description
tableName str Name of the table to create. Can be qualified with catalog name.
path str, optional The path in which the data for this table exists. When path is specified, an external table is created from the data at the given path. Otherwise a managed table is created.
source str, optional The source of this table such as 'parquet', 'orc', etc. If source is not specified, the default data source configured by spark.sql.sources.default will be used.
schema StructType, optional The schema for this table.
description str, optional The description of this table.
**options dict, optional Extra options to specify in the table.

Returns

DataFrame

The DataFrame associated with the table.

Examples

# Creating a managed table.
_ = spark.catalog.createTable("tbl1", schema=spark.range(1).schema, source='parquet')
_ = spark.sql("DROP TABLE tbl1")

# Creating an external table.
import tempfile
with tempfile.TemporaryDirectory(prefix="createTable") as d:
    _ = spark.catalog.createTable(
        "tbl2", schema=spark.range(1).schema, path=d, source='parquet')
_ = spark.sql("DROP TABLE tbl2")