<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Structured streaming - missing records in Gold layer, the foreach batch doesn't write some data in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/structured-streaming-missing-records-in-gold-layer-the-foreach/m-p/49741#M28612</link>
    <description>&lt;P&gt;Good afternoon,Spark,Streaming,Delta,Gold&lt;/P&gt;&lt;P&gt;I'm facing an issue with the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;foreach batch&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;function in my streaming pipeline. The pipeline is fetching data from the data lake storage using Autoloader. This data is first written to a bronze layer. Following this, I explode the JSON files and store the result in the silver layer (structured streaming). From the silver layer, I extract only the required data, perform a join operation with the static table, and then write these changes to the gold layer using the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;foreach batch&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;function.&lt;/P&gt;&lt;P&gt;I have activated the change feed on the gold layer, and these changes are subsequently written to Cosmos DB.&lt;/P&gt;&lt;P&gt;The problem I'm encountering is that certain change events aren't getting written to the gold layer. It appears that this issue arises in a specific scenario: when there hasn't been a change made yet (for example today), the subsequent change will be written. However, if another change follows within the same day, it doesn't get recorded.&lt;/P&gt;&lt;P&gt;Below are the details of my merge function and the delta log. While the batch processing seems to be running (as indicated by the delta log history), the expected changes aren't reflected in the output.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Kristin_0-1698065953782.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/4541iD393BA9603773594/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="Kristin_0-1698065953782.png" alt="Kristin_0-1698065953782.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Function to upsert microBatchOutputDF into Delta table using merge
def upsertToDelta(microBatchOutputDF, batchId):
  #print(microBatchOutputDF)
  (targetDF.alias("t").merge(
    microBatchOutputDF
      .alias("s"),
      .withColumn('row', row_number().over(Window.partitionBy("PARTNER_ID").orderBy("ANSCHRIFT")))
      .filter(col('row') == 1)
      .drop('row'),
    "s.PARTNER_ID = t.PARTNER_ID")
   .whenMatchedUpdate(set =
    {
      "t.LAST_NAME": "s.LAST_NAME",
      "t.FIRST_NAME": "s.FIRST_NAME",
      "t.ANSCHRIFT": "s.ANSCHRIFT",
      "t.POST_CODE": "s.POST_CODE",
      "t.CITY": "s.CITY",
      "t.STREET": "s.STREET",
      "t.HOUSE_NUM1": "s.HOUSE_NUM2",
      "t.ADDRESS_TYPE": "s.ADDRESS_TYPE",
      "t.PARTNER_TYPE": "s.PARTNER_TYPE",
      "t.EMAILS": "s.EMAILS",
      "t.TEL": "s.TEL",
      "t.BANK_DETAILS": "s.BANK_DETAILS",
      "t.id": "s.id",
      "t.document_type": "s.document_type",
      "t.last_update": "s.last_update"
    }
  )
    .whenNotMatchedInsertAll()
    .execute()
  )


def writeStreamGold(sourceDF):
  checkpoint = '/mnt/gold/business_partner'+ "/checkpoint"
  # Write the output of a streaming aggregation query into Delta table
  query = (sourceDF.writeStream
   .format("delta")
  .foreachBatch(upsertToDelta).option("checkpointLocation", checkpoint)
  .outputMode("append")
  .queryName(f"toGold_GP").start()
  )

  return query&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 23 Oct 2023 13:54:57 GMT</pubDate>
    <dc:creator>Kristin</dc:creator>
    <dc:date>2023-10-23T13:54:57Z</dc:date>
    <item>
      <title>Structured streaming - missing records in Gold layer, the foreach batch doesn't write some data</title>
      <link>https://community.databricks.com/t5/data-engineering/structured-streaming-missing-records-in-gold-layer-the-foreach/m-p/49741#M28612</link>
      <description>&lt;P&gt;Good afternoon,Spark,Streaming,Delta,Gold&lt;/P&gt;&lt;P&gt;I'm facing an issue with the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;foreach batch&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;function in my streaming pipeline. The pipeline is fetching data from the data lake storage using Autoloader. This data is first written to a bronze layer. Following this, I explode the JSON files and store the result in the silver layer (structured streaming). From the silver layer, I extract only the required data, perform a join operation with the static table, and then write these changes to the gold layer using the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;foreach batch&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;function.&lt;/P&gt;&lt;P&gt;I have activated the change feed on the gold layer, and these changes are subsequently written to Cosmos DB.&lt;/P&gt;&lt;P&gt;The problem I'm encountering is that certain change events aren't getting written to the gold layer. It appears that this issue arises in a specific scenario: when there hasn't been a change made yet (for example today), the subsequent change will be written. However, if another change follows within the same day, it doesn't get recorded.&lt;/P&gt;&lt;P&gt;Below are the details of my merge function and the delta log. While the batch processing seems to be running (as indicated by the delta log history), the expected changes aren't reflected in the output.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Kristin_0-1698065953782.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/4541iD393BA9603773594/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="Kristin_0-1698065953782.png" alt="Kristin_0-1698065953782.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Function to upsert microBatchOutputDF into Delta table using merge
def upsertToDelta(microBatchOutputDF, batchId):
  #print(microBatchOutputDF)
  (targetDF.alias("t").merge(
    microBatchOutputDF
      .alias("s"),
      .withColumn('row', row_number().over(Window.partitionBy("PARTNER_ID").orderBy("ANSCHRIFT")))
      .filter(col('row') == 1)
      .drop('row'),
    "s.PARTNER_ID = t.PARTNER_ID")
   .whenMatchedUpdate(set =
    {
      "t.LAST_NAME": "s.LAST_NAME",
      "t.FIRST_NAME": "s.FIRST_NAME",
      "t.ANSCHRIFT": "s.ANSCHRIFT",
      "t.POST_CODE": "s.POST_CODE",
      "t.CITY": "s.CITY",
      "t.STREET": "s.STREET",
      "t.HOUSE_NUM1": "s.HOUSE_NUM2",
      "t.ADDRESS_TYPE": "s.ADDRESS_TYPE",
      "t.PARTNER_TYPE": "s.PARTNER_TYPE",
      "t.EMAILS": "s.EMAILS",
      "t.TEL": "s.TEL",
      "t.BANK_DETAILS": "s.BANK_DETAILS",
      "t.id": "s.id",
      "t.document_type": "s.document_type",
      "t.last_update": "s.last_update"
    }
  )
    .whenNotMatchedInsertAll()
    .execute()
  )


def writeStreamGold(sourceDF):
  checkpoint = '/mnt/gold/business_partner'+ "/checkpoint"
  # Write the output of a streaming aggregation query into Delta table
  query = (sourceDF.writeStream
   .format("delta")
  .foreachBatch(upsertToDelta).option("checkpointLocation", checkpoint)
  .outputMode("append")
  .queryName(f"toGold_GP").start()
  )

  return query&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Oct 2023 13:54:57 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/structured-streaming-missing-records-in-gold-layer-the-foreach/m-p/49741#M28612</guid>
      <dc:creator>Kristin</dc:creator>
      <dc:date>2023-10-23T13:54:57Z</dc:date>
    </item>
  </channel>
</rss>

