spark - scala - How can I check if a table exists in hive

We also can define it with a database name as below.

1.6.x

sqlContext.tableNames("db_name").contains("tbl_name")

2.x:

spark.catalog.tableExists("db_name", "tbl_name")


1.x:

def tableExists(table: String, sqlContext: SQLContext) =
  sqlContext.tableNames.contains(table)

2.x:

def tableExists(table: String, spark: SparkSession) =
  spark.catalog.tableExists(table)

2.1.x or later.

You can use spark.catalog.tableExists. Credits go to Huseyin Oktay for pointing that out.