'How do I check the table exists in the database before reading it?

I have a brand new database, and when I want to read my sampleTable, I will receive the exception about PSQLException: Error: relation "sampleTable" does not exist.

Here is my sample code.

val spark = SparkSession.builder().getOrCreate()
val connection = new properties()
connection.setProperty("user", "myUser")
connection.setProperty("password", "myPassword")
connection.setProperty("driver", "org.postgresql.Driver")

val df = spark.read.jdbc("jdbc:postgresql:database", "sampleTable", connection)

Is there any way that can do getOrCreate() for an empty table?

Something I want to do looks like this.

var df = null
if( sampleTable_is_existing ){
    df = spark.read.jdbc("jdbc:postgresql:database", "sampleTable", connection)
}else{
    df = spark.create("sampleTable")
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source