<?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 Re: COPY INTO generating duplicate rows in Delta table in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18190#M12024</link>
    <description>&lt;P&gt;@Umar Ayub​, What you need is MERGE INTO, not COPY INTO.&lt;/P&gt;&lt;P&gt;With MERGE you can specify what records to insert, update, and delete.&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.microsoft.com/en-us/azure/databricks/spark/latest/spark-sql/language-manual/delta-merge-into" alt="https://docs.microsoft.com/en-us/azure/databricks/spark/latest/spark-sql/language-manual/delta-merge-into" target="_blank"&gt;https://docs.microsoft.com/en-us/azure/databricks/spark/latest/spark-sql/language-manual/delta-merge-into&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 09 Jun 2022 07:22:56 GMT</pubDate>
    <dc:creator>-werners-</dc:creator>
    <dc:date>2022-06-09T07:22:56Z</dc:date>
    <item>
      <title>COPY INTO generating duplicate rows in Delta table</title>
      <link>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18189#M12023</link>
      <description>&lt;P&gt;Hello Everyone,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm trying to bulk load tables from a SQL server database into ADLS as parquet files and then loading these files into Delta tables (raw/bronze). I had done a one off history/base load but my subsequent incremental loads (which had a date overlap with history load) are generating duplicates. Some reading I've done so far is pointing out that using native python code such as 'for' loop isn't recommended in Databricks - I can work around this by appending all the SQL statements together and execute with single statement but wanted to know if this was the case and why? Thank you. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;UPDATE: I've removed the code from the for loop and tried execution for a single table and it seems issue isn't with the 'for' loop. If the record was loaded during history load and occurs again in the incremental load but the row hasn't changed at all - it is still getting loaded into the table as a duplicate row. Do the incremental load files need to be mutually exclusive i.e. not contain any overlapping rows with previous files?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Code as below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;spark.sql("USE raw;")
&amp;nbsp;
files = dbutils.fs.ls(landingZoneLocation)
&amp;nbsp;
for fi in files: 
  if fi.isFile:
    delta_table_name = "crm_" + fi.name.split('.')[0].lower()
    deltaTableCopyIntoSQL = "COPY INTO delta.`dbfs:/mnt/raw/"+delta_table_name+"` FROM 'dbfs:/mnt/landing/crm/"+processDate+"/"+fi.name+ "' FILEFORMAT = PARQUET;"
    print(deltaTableCopyIntoSQL) 
    spark.sql(deltaTableCopyIntoSQL)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jun 2022 21:10:11 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18189#M12023</guid>
      <dc:creator>dataexplorer</dc:creator>
      <dc:date>2022-06-08T21:10:11Z</dc:date>
    </item>
    <item>
      <title>Re: COPY INTO generating duplicate rows in Delta table</title>
      <link>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18190#M12024</link>
      <description>&lt;P&gt;@Umar Ayub​, What you need is MERGE INTO, not COPY INTO.&lt;/P&gt;&lt;P&gt;With MERGE you can specify what records to insert, update, and delete.&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.microsoft.com/en-us/azure/databricks/spark/latest/spark-sql/language-manual/delta-merge-into" alt="https://docs.microsoft.com/en-us/azure/databricks/spark/latest/spark-sql/language-manual/delta-merge-into" target="_blank"&gt;https://docs.microsoft.com/en-us/azure/databricks/spark/latest/spark-sql/language-manual/delta-merge-into&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2022 07:22:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18190#M12024</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2022-06-09T07:22:56Z</dc:date>
    </item>
    <item>
      <title>Re: COPY INTO generating duplicate rows in Delta table</title>
      <link>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18191#M12025</link>
      <description>&lt;P&gt;thanks for the guidance!&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2022 22:26:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18191#M12025</guid>
      <dc:creator>dataexplorer</dc:creator>
      <dc:date>2022-06-09T22:26:31Z</dc:date>
    </item>
    <item>
      <title>Re: COPY INTO generating duplicate rows in Delta table</title>
      <link>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18192#M12026</link>
      <description>&lt;P&gt;According to the documentation, COPY INTO should not be inserting duplicated records, it should load a file only once. A caveat seems to be that you have to have loaded all initial data with COPY INTO, rather than CREATE the table with SELECT on some initial batch of files. I at least tried to update an existing table with new parquet files from the same S3 storage location, and the first run of COPY INTO duplicated everything. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Further, MERGE INTO does not appear to support merging from parquet files, so if I want to use that, I likely have to create a staging table in Delta. Sigh.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Oct 2022 06:30:49 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18192#M12026</guid>
      <dc:creator>652852</dc:creator>
      <dc:date>2022-10-04T06:30:49Z</dc:date>
    </item>
    <item>
      <title>Re: COPY INTO generating duplicate rows in Delta table</title>
      <link>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18193#M12027</link>
      <description>&lt;P&gt;copy into itself does not insert dups, but it is an append operation. So if identical data already resides in the table you will have dups after the copy into.&lt;/P&gt;&lt;P&gt;Merge is specifically made to tackle that.&lt;/P&gt;&lt;P&gt;And it does support merging from parquet files.  Almost all of my merges have parquet as a source.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Oct 2022 07:23:52 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18193#M12027</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2022-10-04T07:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: COPY INTO generating duplicate rows in Delta table</title>
      <link>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18194#M12028</link>
      <description>&lt;P&gt;The documentation states clearly that "This is a retriable and idempotent operation—files in the source location that have already been loaded are skipped." What it doesn't say is that this file load history is apparently specific to the COPY INTO operation--so in practice loading even the very first batch of data with COPY INTO is necessary to have a complete loading history and avoid reloading on subsequent runs.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But, I'll happily use merge, though I haven't found correct syntax to load with parquet -- can you please share example code using MERGE INTO and parquet?&lt;/P&gt;</description>
      <pubDate>Tue, 04 Oct 2022 07:55:03 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18194#M12028</guid>
      <dc:creator>652852</dc:creator>
      <dc:date>2022-10-04T07:55:03Z</dc:date>
    </item>
    <item>
      <title>Re: COPY INTO generating duplicate rows in Delta table</title>
      <link>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18195#M12029</link>
      <description>&lt;P&gt;Correct!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There is no specific syntax for parquet.&lt;/P&gt;&lt;P&gt;You just read the source data (the new incoming records) into a spark dataframe.&lt;/P&gt;&lt;P&gt;Use that dataframe for the merge (if you use sql, first create a temp view on the dataframe).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Oct 2022 08:09:38 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/copy-into-generating-duplicate-rows-in-delta-table/m-p/18195#M12029</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2022-10-04T08:09:38Z</dc:date>
    </item>
  </channel>
</rss>

