I have tried multiple ways to incremental load (upsert) into the Postgress Database (RDS) using Spark (with Glue Job) but did not find satisfactory performance.
I have tried the below ways:
1. Delete the records based on the primary Key and append new records
In this approach, I
deletethe records where theprimary keyexists in incremental data. This is done by Python API such aspg8000etc. Once it gets successfully completed, I write data usingSparkinappendmode.This approach works very smooth when the incremental load is less but with high volumes, Delete and writing takes time.
2. Writing data into a staging table and triggering DB store procedure
I have also tried this alternative way to handle the
upsertwith arelational database. Here I am writing data into a Staging table usingSparkand triggeringStore Procedure(withpg8000). The Store Procedure willupsertthe data from the staging table to the final table.With the approach, we have to manage multiple
Store procedureand also have to depend on theDatabase(in case we are not owning that system). Handling the staging to final upsert will be tricky if the previous job gets failed after writing data into staging and before triggering the Store procedure. Overall I feel this approach needs good orchestration not only with the Spark but with Store procedure as well.
Is there any way that can handle upsert in a relational database (Postgres/MySQL/Oracle etc) very smoothly and efficiently using Spark?