Simple Python code sample that checks if a Hive database or table exists.
from pyspark.sql import HiveContext
def database_exists(hc, db):
"""
Function that checks the existence of a Hive database
:param hc: Hive Context
:param db: database name
:return: bool, True if dabase exists
"""
return bool([x.datadaseName for x in hc.sql("SHOW DATABASES").collect() if x.datadaseName == db])
def table_exists(hc, db, table):
"""
Function that checks the existence of a Hive table
:param hc: Hive Context
:param db: database name
:param table: table name
:return: bool, True if table exists
"""
if database_exists(hc=hc, db=db):
hc.sql("USE %s" % (db,))
return table in hc.tableNames()
else:
return False
This iis a great post
LikeLike