如何检查在PySpark中是否存在临时视图?

fnatzsnv  于 2022-10-07  发布在  Spark
关注(0)|答案(3)|浏览(292)

我了解如何在PySpark中检查表的存在:

>>> spark.catalog.setCurrentDatabase("staging")
>>> 'test_table' in sqlContext.tableNames()
True

但是风景呢?如果它是这样创建的:

df = sqlContext.sql("SELECT * FROM staging.test_table")
df.createOrReplaceTempView("test_view")
df.persist(p.persistLevel)

如何检查稍后的代码中是否存在“测试视图”?

bprjcwpo

bprjcwpo1#

您可以使用sqlConext.ableNames和sqlConext.ables

>>> sqlContext.registerDataFrameAsTable(df, "table1")
>>> "table1" in sqlContext.tableNames()
True
>>> "table1" in sqlContext.tableNames("default")
True
ezykj2lf

ezykj2lf2#

“默认”是定义视图的上下文。

>>> spark.catalog.setCurrentDatabase("staging")
>>> 'test_view' in sqlContext.tableNames()
False
>>> spark.catalog.setCurrentDatabase("default")
>>> 'test_view' in sqlContext.tableNames()
True

这需要一点时间(>3秒)

试一试/抓住它会更快

try:
  _=spark.read.table('test_view')
  print('Exists!')
catch:
  print('Does not exist.')
wkyowqbh

wkyowqbh3#

以下是我使用spark.catalog.tableExists()时的用法:

from datetime import datetime, date
from pyspark.sql import SparkSession, Row

spark = SparkSession.builder.getOrCreate()

df = spark.createDataFrame([
    Row(a=1, b=2., c='string1', d=date(2000, 1, 1), e=datetime(2000, 1, 1, 12, 0)),
    Row(a=2, b=3., c='string2', d=date(2000, 2, 1), e=datetime(2000, 1, 2, 12, 0)),
    Row(a=4, b=5., c='string3', d=date(2000, 3, 1), e=datetime(2000, 1, 3, 12, 0))
])

# create a view

df.createTempView('demo')

# check if this view exists

print(spark.catalog.tableExists('demo'))

相关问题