Verifying if a Hive database or table exists

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: 
        False

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s