I'm using the following simple line of code in to load data to SQL DB from Azure Databricks
val loadsqldb = spark.sql("""SELECT * FROM TABLEA""")
// WRITE FROM CONFIG
val writeConfig = Config(Map(
"url" -> url,
"databaseName" -> databaseName,
"dbTable" -> "dbo.TABLENAME",
"user" -> user,
"password" -> password,
"connectTimeout" -> "5"
))
//~
loadsqldb.write.mode(SaveMode.Overwrite).option("truncate", true).sqlDB(writeConfig)
We have a unique ID key on our server which we MUST retain which looks like the following:
CREATE UNIQUE INDEX i__NeighbourhoodCategoryHourlyForecast_unique
ON almanac_devdb.dbo.NeighbourhoodCategoryHourlyForecast (fk_neighbourhoods, fk_categories, local_date, local_hour)
GO
When I try to load data to our SQL DB we get the following error;
Cannot insert duplicate key row in object 'dbo.TABLENAME' with unique index 'i__TABLENAME_unique'. The duplicate key value is (36983, 130000, 2020-08-12, 14).
It was suggested that I found someway to get Databricks to do a MERGE into of OVERWRITE, but I'm not sure how to do that or even if that is the correct thing to do?