cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Update target column with optional source columns

RUC
New Contributor

I am trying to load on prem transaction log table and update a databricks table.In Source table,the column reqdetails table hold all the information and expect ProductID remaining columns are dynamic(not all columns exists in the request) all time the optional/nullable.Could you please let me the process using python in databricks ? Explored StructType but it needs mandatory specific columns.

Source Table

Req IDTypeReq DetailsStatus
1UpdateProductID=234;ProductName=LawnMover;Price=58True
2UpdateProductID=874;Price=478True
3UpdateProductID=678;ProductParentgroup=Watersuppuly;Price=1.6True

Target table before Update

ProductIDProductParentgroupProductNamePrice
234UtilityMover86
874HOASink450
678WaterFilters1.2

Target table after Update

ProductIDProductParentgroupProductNamePrice
234UtilityLawnMover58
874HOASink478
678WatersupplyFilters1.6

 

1 REPLY 1

Brahmareddy
Honored Contributor II

Hi Ruc

How are you doing today?, as per my understanding, Since your source table has a ReqDetails column with dynamic key-value pairs and only ProductID is fixed, you can parse the ReqDetails column dynamically and merge it into the target table using PySpark and Delta Lake. First, read the source data into a DataFrame and use a UDF (User-Defined Function) to split ReqDetails into a dictionary of key-value pairs. Then, extract only the available keys (e.g., ProductName, Price, ProductParentgroup) and create new columns dynamically. Finally, use Delta Lake’s MERGE operation to update only the modified values in the target table, using COALESCE() to retain existing data when a column is missing in the request. This way, you avoid a rigid schema while ensuring that updates happen correctly. Hope this helps!

Regards,

Brahma

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!

Sign Up Now