Forum Discussion
Issue with Upsert Table Action in Metadata-Driven Pipeline to Lakehouse Table
- 1 year ago
Hi parasol54
Warehouse tables in Fabric Data Warehouse = Lakehouse Tables.
That means Whatever you are doing in Lakehouse ( such as creating Delta table) is also applies to Data Warehouse Tables. ( Unified Data )
Lakehouse Delta Tables
Data Warehouse Tables
No limitation applies. Whether you create a Lakehouse or Warehouse Tables. I would highly recommend you to use Notebooks and Delta Tables. That way you would understand Data Lake and how it migrated to Delta Lake.
- 1 year ago
Hi parasol54 ,
Thanks for reaching out to the Microsoft fabric community forum.
lbendlin ,
Thanks for your prompt response
Since the Copy Activity in Microsoft Fabric doesn’t currently support the 'Upsert' action for Lakehouse Delta tables, a practical workaround is to load your data into a staging table using 'Overwrite' mode, then use a notebook (SQL or Python) to perform a merge operation into your destination table based on defined key columns this approach simulates upsert behavior by updating matching records and inserting new ones, and it integrates well into a metadata-driven pipeline if you extend your control table to include merge rules.
Lakehouse and Delta Tables - Microsoft Fabric | Microsoft Learn
Options to get data into the Lakehouse - Microsoft Fabric | Microsoft Learn
Best Regards,
Lakshmi Narayana
Hello parasol54 -
Here is an example of how you can perform upserts on lakehouse tables within a metadata-driven pipline.
- Run a notebook which contains this code below.
Create variables which define the paths (parameters for each component are passed to the notebook from the data pipeline):
# build absolute paths to lakehouse storage locations
source__path = f"{source__protocol}://{source__storage_account}@{source__endpoint}/{source__container}/{source__directory}/"
destination__path = f"{destination__protocol}://{destination__storage_account}@{destination__endpoint}/{destination__container}/{destination__directory}/"
Define a function to perform the upsert.
def process_table(source__lakehouse_path, source__table_name, destination__lakehouse_path, destination__table_name, unique_id):
# load the tables
source__table = DeltaTable.forPath(source__lakehouse_path+source__table_name)
destination__table = DeltaTable.forPath(destination__lakehouse_path+destination__table_name)
# convert the source table to a dataframe
df = source__table.toDF()
# define a list of columns names
column_names = df.columns
# perform the merge (upsert) operation
destination__table.alias('destination') \
.merge(
df.alias('updates'),
'destination.{0} = updates.{0}'.format(unique_id)
)\
.whenMatchedUpdate(set = {col: f"updates.{col}" for col in column_names}) \
.whenNotMatchedInsert(values = {col: f"updates.{col}" for col in column_names})\
.execute()
Invoke the function for each row in the metadata/control/watermark table (json_array)
for i in json_array:
source__table_name = i["source__table"]
destination__table_name = i["destination__table"]
unique_id = i["unique_id"]
process_table(source__path, source__table_name, destination__path, destination__table_name, unique_id)