Ref:https://issues.apache.org/jira/browse/SPARK-11953
In Spark 1.3.1 we have 2 methods i.e.. CreateJdbcTable and InsertIntoJdbc
They are replaced with write.jdbc() in Spark 1.4.1
CreateJDBCTable allows to perform CREATE TABLE ... i.e... DDL on the table
followed by INSERT (DML)
InsertIntoJDBC will avoid performing DDL on the table and INSERT (DML)
In Spark 1.4.1 both of the above technologies are replaced by write.jdbc.
When we want to Insert data into the table that already exists, I am
passing SaveMode equals to Append.
When I say SaveMode equals to Mode, I would like to by pass
1) tableExists check
2) Avoid Spark CREATE table in a scenario when there is no Table available
in the Table
Please let me know if you think differently.
Regards
Shiv
def jdbc(url: String, table: String, connectionProperties: Properties):
Unit = {
val conn = JdbcUtils.createConnection(url, connectionProperties)
try {
var tableExists = JdbcUtils.tableExists(conn, table)
if (mode == SaveMode.Ignore && tableExists)
{ return }
if (mode == SaveMode.ErrorIfExists && tableExists)
{ sys.error(s"Table $table already exists.") }
if (mode == SaveMode.Overwrite && tableExists)
{ JdbcUtils.dropTable(conn, table) tableExists = false }
// Create the table if the table didn't exist.
if (!tableExists)
{ val schema = JDBCWriteDetails.schemaString(df, url) val sql = s"CREATE
TABLE $table ($schema)" conn.prepareStatement(sql).executeUpdate() }
} finally
{ conn.close() }
JDBCWriteDetails.saveTable(df, url, table, connectionProperties)
}