<?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 Databricks SDP (Spark declarative Pipelins) Overwrite table in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/databricks-sdp-spark-declarative-pipelins-overwrite-table/m-p/170550#M56316</link>
    <description>&lt;DIV&gt;1) Consider I have orders folder which orders_1.csv file&amp;nbsp; and it is loaded to orders tables&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;orders/&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;============&amp;gt; load to order table&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;orders_1.csv&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;2)&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;next day new file (orders_2.csv) arrived&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;orders/&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;=========&amp;gt; take only 2nd file -----&amp;gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;overwrite the order table&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;orders_1.csv&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; orders_2.csv&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Here streaming table only appending the data to orders table.&amp;nbsp; Materialized view is considering both files. &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;My requirement is overwrite the orders table.&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Can you help me how to do that ??&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;</description>
    <pubDate>Sun, 04 Oct 2026 15:21:10 GMT</pubDate>
    <dc:creator>pvrcloudtech</dc:creator>
    <dc:date>2026-10-04T15:21:10Z</dc:date>
    <item>
      <title>Databricks SDP (Spark declarative Pipelins) Overwrite table</title>
      <link>https://community.databricks.com/t5/data-engineering/databricks-sdp-spark-declarative-pipelins-overwrite-table/m-p/170550#M56316</link>
      <description>&lt;DIV&gt;1) Consider I have orders folder which orders_1.csv file&amp;nbsp; and it is loaded to orders tables&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;orders/&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;============&amp;gt; load to order table&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;orders_1.csv&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;2)&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;next day new file (orders_2.csv) arrived&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;orders/&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;=========&amp;gt; take only 2nd file -----&amp;gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;overwrite the order table&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;orders_1.csv&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; orders_2.csv&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Here streaming table only appending the data to orders table.&amp;nbsp; Materialized view is considering both files. &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;My requirement is overwrite the orders table.&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Can you help me how to do that ??&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Sun, 04 Oct 2026 15:21:10 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/databricks-sdp-spark-declarative-pipelins-overwrite-table/m-p/170550#M56316</guid>
      <dc:creator>pvrcloudtech</dc:creator>
      <dc:date>2026-10-04T15:21:10Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks SDP (Spark declarative Pipelins) Overwrite table</title>
      <link>https://community.databricks.com/t5/data-engineering/databricks-sdp-spark-declarative-pipelins-overwrite-table/m-p/170552#M56317</link>
      <description>&lt;P&gt;Streaming tables are append-only by design, so they can't overwrite. Use a materialized view that keeps only the rows from the newest file, via the _metadata column:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;sql&lt;/P&gt;&lt;P&gt;CREATE OR REFRESH MATERIALIZED VIEW orders AS&lt;/P&gt;&lt;P&gt;SELECT * EXCEPT (file_time)&lt;/P&gt;&lt;P&gt;FROM (&lt;/P&gt;&lt;P&gt;&amp;nbsp; SELECT *,&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_metadata.file_modification_time AS file_time&lt;/P&gt;&lt;P&gt;&amp;nbsp; FROM read_files('/path/orders/', format =&amp;gt; 'csv', header =&amp;gt; true)&lt;/P&gt;&lt;P&gt;)&lt;/P&gt;&lt;P&gt;QUALIFY file_time = MAX(file_time) OVER ();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Each refresh then replaces the table contents with only the latest file (orders_2.csv, then orders_3.csv, and so on).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If the folder grows large, ingest with a streaming table (Auto Loader) into a bronze table, storing _metadata.file_name and _metadata.file_modification_time as columns, and build the same "latest file only" materialized view on top of it. That way all the files aren't re-read on every refresh.&lt;/P&gt;&lt;P&gt;Hope this helps&lt;/P&gt;</description>
      <pubDate>Sun, 04 Oct 2026 16:16:50 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/databricks-sdp-spark-declarative-pipelins-overwrite-table/m-p/170552#M56317</guid>
      <dc:creator>SumeshKashyap</dc:creator>
      <dc:date>2026-10-04T16:16:50Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks SDP (Spark declarative Pipelins) Overwrite table</title>
      <link>https://community.databricks.com/t5/data-engineering/databricks-sdp-spark-declarative-pipelins-overwrite-table/m-p/170554#M56318</link>
      <description>&lt;P&gt;A Streaming Table may not be the right fit for this requirement. It is designed to process new files incrementally, so when &lt;STRONG&gt;orders2.csv&amp;nbsp;&lt;/STRONG&gt;arrives, it will append the new data.&lt;/P&gt;&lt;P&gt;If each new file is a full snapshot and should completely replace the previous data, you can use a batch job to read only the latest file and overwrite the target Delta table using:&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;df.write.mode("overwrite").saveAsTable("orders")&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Another option is to keep only the latest file in a&amp;nbsp; &lt;STRONG&gt;current&lt;/STRONG&gt;/ folder and move older files to an &lt;STRONG&gt;archive&lt;/STRONG&gt;/ folder.&lt;/P&gt;&lt;P&gt;So in this case, I would prefer a batch overwrite approach rather than a Streaming Table.&lt;/P&gt;</description>
      <pubDate>Sun, 04 Oct 2026 16:25:25 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/databricks-sdp-spark-declarative-pipelins-overwrite-table/m-p/170554#M56318</guid>
      <dc:creator>VK210287</dc:creator>
      <dc:date>2026-10-04T16:25:25Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks SDP (Spark declarative Pipelins) Overwrite table</title>
      <link>https://community.databricks.com/t5/data-engineering/databricks-sdp-spark-declarative-pipelins-overwrite-table/m-p/170660#M56332</link>
      <description>&lt;P data-pm-slice="1 1 []"&gt;If each file is a full snapshot, use AUTO CDC FROM SNAPSHOT with SCD Type 1; it processes files in numeric order and makes the target match the latest snapshot, including deleting absent keys (&lt;A href="https://docs.databricks.com/aws/en/ldp/cdc#example-process-snapshots-using-version-functions" target="_blank"&gt;version-function example&lt;/A&gt;, &lt;A href="https://docs.databricks.com/aws/en/ldp/developer/ldp-python-ref-apply-changes-from-snapshot" target="_blank"&gt;snapshot API&lt;/A&gt;). Use this code in place of the current &lt;CODE&gt;orders&lt;/CODE&gt; definition:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class="language-python"&gt;import re

from pyspark import pipelines as dp

ORDERS_DIR = "/Volumes/&amp;lt;catalog&amp;gt;/&amp;lt;schema&amp;gt;/&amp;lt;volume&amp;gt;/orders/"


def next_snapshot_and_version(latest_version):
    versions = sorted(
        int(m.group(1))
        for f in dbutils.fs.ls(ORDERS_DIR)
        if (m := re.fullmatch(r"orders_(\d+)\.csv", f.name))
    )
    newer = [v for v in versions if latest_version is None or v &amp;gt; latest_version]
    if not newer:
        return None
    df = spark.read.format("csv").option("header", True).load(f"{ORDERS_DIR}orders_{newer[0]}.csv")
    return df, newer[0]


dp.create_streaming_table("orders")
dp.create_auto_cdc_from_snapshot_flow(
    target="orders",
    source=next_snapshot_and_version,
    keys=["order_id"],
    stored_as_scd_type=1,
)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Set &lt;CODE&gt;keys&lt;/CODE&gt; to the column or columns that uniquely identify a row (&lt;A href="https://docs.databricks.com/aws/en/ldp/developer/ldp-python-ref-apply-changes-from-snapshot" target="_blank"&gt;snapshot API&lt;/A&gt;). Without &lt;CODE&gt;.schema(...)&lt;/CODE&gt;, this CSV read returns every column as a string; add the schema before &lt;CODE&gt;.load(...)&lt;/CODE&gt; for typed columns (&lt;A href="https://spark.apache.org/docs/latest/sql-data-sources-csv.html#data-source-option" target="_blank"&gt;CSV options&lt;/A&gt;, &lt;A href="https://docs.databricks.com/aws/en/query/formats/csv#specify-a-schema" target="_blank"&gt;specify a schema&lt;/A&gt;). The flow requires serverless or the Pro or Advanced edition (&lt;A href="https://docs.databricks.com/aws/en/ldp/cdc#requirements" target="_blank"&gt;requirements&lt;/A&gt;). For an existing streaming table, switch the code, then run a full refresh of only &lt;CODE&gt;orders&lt;/CODE&gt;; for an existing materialized view, switch the code, run &lt;CODE&gt;&lt;A href="https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-drop-view" target="_self"&gt;DROP MATERIALIZED VIEW&lt;/A&gt;&lt;/CODE&gt;, then run the same full refresh (&lt;A href="https://docs.databricks.com/aws/en/error-messages/error-classes#cannot_change_dataset_type" target="_blank"&gt;type-change guidance&lt;/A&gt;, &lt;A href="https://docs.databricks.com/aws/en/ldp/multi-file-editor#run-pipeline-code" target="_blank"&gt;full-refresh action&lt;/A&gt;).&lt;/P&gt;</description>
      <pubDate>Mon, 05 Oct 2026 17:28:15 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/databricks-sdp-spark-declarative-pipelins-overwrite-table/m-p/170660#M56332</guid>
      <dc:creator>AbhilashNagilla</dc:creator>
      <dc:date>2026-10-05T17:28:15Z</dc:date>
    </item>
    <item>
      <title>Re: Databricks SDP (Spark declarative Pipelins) Overwrite table</title>
      <link>https://community.databricks.com/t5/data-engineering/databricks-sdp-spark-declarative-pipelins-overwrite-table/m-p/172201#M56595</link>
      <description>&lt;DIV&gt;
&lt;P&gt;Streaming tables are append-only by design, and a materialized view recomputes over everything in the folder. Neither gives you "replace the table with just the newest file" out of the box, so you need a small pattern on top. Here are the two I'd consider.&lt;/P&gt;
&lt;H2&gt;Option 1: Auto Loader + &lt;CODE&gt;foreachBatch&lt;/CODE&gt; overwrite (my preference)&lt;/H2&gt;
&lt;P&gt;Auto Loader tracks which files it has already processed and only picks up new ones on each run [1]. Instead of appending, you overwrite the target table with whatever each batch contains. That means the table always holds only the newly arrived file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class="language-python"&gt;def overwrite_orders(batch_df, batch_id):
    if batch_df.isEmpty():
        return  # nothing new, leave the table untouched
    (batch_df.write
        .mode("overwrite")
        .saveAsTable("my_catalog.my_schema.orders"))

(spark.readStream
    .format("cloudFiles")
    .option("cloudFiles.format", "csv")
    .option("header", "true")
    .option("cloudFiles.schemaLocation", "/Volumes/my_catalog/my_schema/chk/orders_schema")
    .load("/Volumes/my_catalog/my_schema/landing/orders/")
  .writeStream
    .foreachBatch(overwrite_orders)
    .option("checkpointLocation", "/Volumes/my_catalog/my_schema/chk/orders")
    .trigger(availableNow=True)
    .start())
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Run this as a normal job or notebook task, scheduled daily or file-arrival triggered. Don't try to put it inside a declarative pipeline streaming table, because that is the append model you're already seeing.&lt;/LI&gt;
&lt;LI&gt;The &lt;CODE&gt;isEmpty()&lt;/CODE&gt; guard matters. Without it, a run with no new files would overwrite your table with an empty result.&lt;/LI&gt;
&lt;LI&gt;If two files land between runs, one batch will contain both. If you need strictly "only the latest file", filter the batch on &lt;CODE&gt;_metadata.file_modification_time&lt;/CODE&gt; (or &lt;CODE&gt;_metadata.file_path&lt;/CODE&gt;) before writing.&lt;/LI&gt;
&lt;LI&gt;Delta overwrite is atomic, so readers never see a half-empty table, and you keep time travel on the previous version.&lt;/LI&gt;
&lt;/UL&gt;
&lt;H2&gt;Option 2: Materialized view that selects only the latest file&lt;/H2&gt;
&lt;P&gt;If you want to stay fully declarative, keep the materialized view but filter to the newest file using the file metadata column:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class="language-sql"&gt;CREATE OR REFRESH MATERIALIZED VIEW orders AS
WITH src AS (
  SELECT *, _metadata.file_modification_time AS file_ts
  FROM read_files(
    '/Volumes/my_catalog/my_schema/landing/orders/',
    format =&amp;gt; 'csv',
    header =&amp;gt; true
  )
)
SELECT * EXCEPT (file_ts)
FROM src
WHERE file_ts = (SELECT max(file_ts) FROM src);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The trade-off is that the source folder is still scanned on refresh, so as old files pile up the cost grows. If you go this route, archive or delete processed files periodically.&lt;/P&gt;
&lt;H2&gt;What I'd avoid&lt;/H2&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;CODE&gt;TRUNCATE&lt;/CODE&gt; followed by &lt;CODE&gt;COPY INTO&lt;/CODE&gt;. &lt;CODE&gt;COPY INTO&lt;/CODE&gt; is idempotent and skips files it has already loaded [1], so it would load just the new file. But the two steps aren't atomic, and a run with no new file leaves you with an empty table.&lt;/LI&gt;
&lt;LI&gt;Full refresh on the streaming table. It reprocesses everything in the folder, which is the opposite of what you want.&lt;/LI&gt;
&lt;/UL&gt;
&lt;H2&gt;One more thing to check&lt;/H2&gt;
&lt;P&gt;If "overwrite" actually means that rows in the new file should replace matching rows (same &lt;CODE&gt;order_id&lt;/CODE&gt;) rather than wipe the table, you don't want an overwrite at all. You want an upsert, either with &lt;CODE&gt;MERGE INTO&lt;/CODE&gt; inside &lt;CODE&gt;foreachBatch&lt;/CODE&gt; or with the declarative auto CDC flow in a pipeline. Let me know which semantics you need and I can sketch that version.&lt;/P&gt;
&lt;H2&gt;References&lt;/H2&gt;
&lt;P&gt;[1] Ingest data from cloud object storage | Databricks on AWS — &lt;A href="https://docs.databricks.com/aws/en/ingestion/cloud-object-storage" target="_blank"&gt;https://docs.databricks.com/aws/en/ingestion/cloud-object-storage&lt;/A&gt;&lt;/P&gt;
&lt;/DIV&gt;</description>
      <pubDate>Wed, 07 Oct 2026 19:17:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/databricks-sdp-spark-declarative-pipelins-overwrite-table/m-p/172201#M56595</guid>
      <dc:creator>anuj_lathi</dc:creator>
      <dc:date>2026-10-07T19:17:34Z</dc:date>
    </item>
  </channel>
</rss>

