cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with MERGE INTO for first batch

Joe1912
New Contributor III

I have source data with multiple rows and columns, 1 of column is city. 

I want to get unique city into other table by stream data from source table. So I trying to use merge into and foreachBatch with my merge function.  My merge condition is : On source.city = target.city. But then I realized that, in first batch, I will always get duplicated data becauce there is no data on target and duplicated city on first batch of source.

How can I resolve this ? thank you for supporting

1 ACCEPTED SOLUTION

Accepted Solutions

Kaniz
Community Manager
Community Manager

Hi @Joe1912 , 

Certainly, you can handle this issue by:

  1. Create a temporary staging table for your streaming data.

  2. In the first batch, load data into the staging table, including duplicates.

  3. After each batch, remove duplicates from the staging table.

  4. Use mergeInto to merge the source and deduplicated data in the staging table.

This approach ensures that the initial duplicates are removed before merging the data.

View solution in original post

1 REPLY 1

Kaniz
Community Manager
Community Manager

Hi @Joe1912 , 

Certainly, you can handle this issue by:

  1. Create a temporary staging table for your streaming data.

  2. In the first batch, load data into the staging table, including duplicates.

  3. After each batch, remove duplicates from the staging table.

  4. Use mergeInto to merge the source and deduplicated data in the staging table.

This approach ensures that the initial duplicates are removed before merging the data.

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.