SPARK SQL - update MySql table using DataFrames and JDBC

nicola

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:

myDataFrame.write.mode(SaveMode.Append).jdbc(JDBCurl,mySqlTable,connectionProperties)

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

zero323

It is not possible. As for now (Spark 1.6.0 / 2.2.0 SNAPSHOT) 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 no write conflicts (typically by using application specific partitioning) 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spark SQL - load data with JDBC using SQL statement, not table name

From Dev

MySQL UPDATE using target table

From Dev

MySQL UPDATE using target table

From Dev

sql update table using the case

From Dev

sql update table using the case

From Dev

Update sql table using if statements

From Dev

Dynamic sql - update table using table variable

From Dev

Spark SQL Pyspark update value in table to another value in table

From Dev

Spark Dataframes UPSERT to Postgres Table

From Dev

SQL Update beginning of NText in a column in MySQL table

From Dev

Update mysql date fields in table using case

From Dev

UPDATE MySQL table from a CSV using PHP

From Dev

Update mysql table with php using ajax

From Dev

Insert to table or update if exists using MYSQL database

From Dev

Update probability to original table using MySQL?

From Dev

MySQL Update using data from same table

From Dev

how to update a column in a mysql table using sqlachemy?

From Dev

Update mysql date fields in table using case

From Dev

Q: Update value in mysql table using PHP

From Dev

Merging dataframes using a table

From Dev

Transforming two dataframes in spark sql

From Dev

load dynamic data from MySQL table in ElasticSearch using JDBC driver

From Java

Update a table using JOIN in SQL Server?

From Dev

SQL: update table using create statement

From Dev

How to update a table using SQL in a PHP code?

From Dev

How to update table using foreign key in sql

From Dev

How to lock rows for update and then update a table using a variable in MySQL

From Dev

Update parent table with strongest status of child table using MySql

From Dev

How to update a table line by line using another table in mysql

Related Related

HotTag

Archive