Pular para o conteúdo principal

tabelaExiste

Verifique se a tabela ou view com o nome especificado existe. Isso pode ser uma view temporária ou uma tabela/view.

Sintaxe

tableExists(tableName: str, dbName: str = None)

Parâmetros

Parâmetro

Tipo

Descrição

tableName

str

Nome da tabela cuja existência deve ser verificada. Se nenhum banco de dados for especificado, primeiro tente tratar tableName como um identificador de namespace de várias camadas e, em seguida, tente tableName como um nome de tabela normal no banco de dados atual, se necessário. Pode ser qualificado com o nome do catálogo quando dbName for None.

dbName

str, opcional

Nome do banco de dados no qual a tabela deverá ser verificada.

Devoluções

bool

Indica se a tabela/view existe.

Exemplos

Python
# Check if a table is defined or not.
spark.catalog.tableExists("unexisting_table")
# False
_ = spark.sql("DROP TABLE IF EXISTS tbl1")
_ = spark.sql("CREATE TABLE tbl1 (name STRING, age INT) USING parquet")
spark.catalog.tableExists("tbl1")
# True

# Using the fully qualified names for tables.
spark.catalog.tableExists("default.tbl1")
# True
spark.catalog.tableExists("spark_catalog.default.tbl1")
# True
spark.catalog.tableExists("tbl1", "default")
# True
_ = spark.sql("DROP TABLE tbl1")

# Check if views exist.
spark.catalog.tableExists("view1")
# False
_ = spark.sql("CREATE VIEW view1 AS SELECT 1")
spark.catalog.tableExists("view1")
# True

# Check if temporary views exist.
_ = spark.sql("CREATE TEMPORARY VIEW view1 AS SELECT 1")
spark.catalog.tableExists("view1")
# True
df = spark.sql("DROP VIEW view1")
spark.catalog.tableExists("view1")
# False