Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 09:48 AM
Hi,
The code synchronizes Databricks with PostgreSQL by identifying differences and applying INSERT, UPDATE, or DELETE operations to update PostgreSQL. The steps are as follows:
- Read the source data in Databricks using a simple spark.sql query.
- Read the data from PostgreSQL using the JDBC driver.
- Perform a JOIN operation to identify differences.
- Collect the data using .collect() (I am now trying to use .toLocalIterator()).
- Chunk the data and iterate over it, executing DML operations using psycopg2 in batch (extras.execute_batch()), pushing a list of tuples with page_size=1000.
- …and that’s all.
Could the issue be that psycopg2 is not an API call from Databricks, so execution is handled by the driver? Or is the .collect() operation causing a bottleneck by bringing too much data to the driver at once?