Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have to check whether a table exists in hive using spark(1.6.2) scala

If it doesn't I have to create an empty dataframe and save that as a hive table.

If it exists, then overwrite the existing table.

I need a function that returns boolean based on which I could take the above mentioned decisions(of whether to create new table or overwrite existing one)

like image 417
Uday Sagar Avatar asked Sep 28 '17 19:09

Uday Sagar


1 Answers

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.

like image 68
Alper t. Turker Avatar answered Sep 22 '22 20:09

Alper t. Turker