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: 

Append to a record when updating Delta Table

Serhii
Contributor

I am updating the delta table in Databricks as follows

    segments_data.alias('segments_old').merge(
        segments_data_new.alias("updates"),
        "segments_old.source_url = updates.source_url",
    ).whenMatchedUpdate(
        set={"segments": "segments_old.segments"  + "updates.segments"  } # <- the line is pseudocode, don't know correct API
    ).whenNotMatchedInsertAll().execute()

where "segments" is a list of StructType

    StructField(
      "segments",
      StructType(
        [ ....

How can I append to the existing list - namely append to "segments" column in segments_old from updates within "whenMatchedUpdate"?

Instead of replacing - say "segments": "updates.segments" I would like to append "segments": "segments_old.segments" + "updates.segments"

Thanks for suggestions in advance!

3 REPLIES 3

Hubert-Dudek
Esteemed Contributor III

Set is just the equivalent of SQL UPDATE SET, so it sets a value from one table to a value from the expression.

So you can use built-in functions like col or exp:

from pyspark.sql.functions import *
...
set = { 'gender': col('segments_old.segments') + col('updates.segments') }
OR
set = { 'gender': exp('segments_old.segments + updates.segments') }

image.png 

Anonymous
Not applicable

Hi @Sergii Ivakhno​ 

Hope all is well! Just wanted to check in if you were able to resolve your issue and would you be happy to share the solution or mark an answer as best? Else please let us know if you need more help. 

We'd love to hear from you.

Thanks!

Serhii
Contributor

Yes it worked for us - thanks 🙂

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group