Explore Courses Blog Tutorials Interview Questions
0 votes
in Big Data Hadoop & Spark by (11.4k points)

I'm trying to insert and update some data on MySql using Spark SQL DataFrames and JDBC connection.

I've succeeded to insert new data using the SaveMode.Append. Is there a way to update the data already existing in MySql Table from Spark SQL?

My code to insert is:


If I change to SaveMode.Overwrite it deletes the full table and creates a new one, I'm looking for something like the "ON DUPLICATE KEY UPDATE" available in MySql

P.S. - consider

Spark 1.6.0 / 2.2.0 SNAPSHOT

1 Answer

0 votes
by (32.3k points)

It is not actually possible. As for Spark 1.6.0+ Spark DataFrameWriter supports only four writing modes:

  • SaveMode.Overwrite: overwrite the existing data.

  • SaveMode.Append: append the data.

  • SaveMode.Ignore: ignore the operation (i.e. no-op).

  • SaveMode.ErrorIfExists: default option, throw an exception at runtime.

You can insert manually for example using mapPartitions (since you want an UPSERT operation should be idempotent and as such easy to implement), write to temporary table and execute upsert manually, or use triggers.

In general achieving upsert behavior for batch operations and keeping decent performance is far from trivial. You have to remember that in general case there will be multiple concurrent transactions in place (one per each partition) so you have to ensure that there will be no write conflicts (typically when application specific partitioning is used) or provide appropriate recovery procedures. In practice it may be better to perform and batch writes to a temporary table and resolve upsert part directly in the database.

Browse Categories