Sub topic 1
What Batch Ingestion Is and Where COPY INTO Fits
Prerequisites
This chapter assumes you already have the following:
- Basic SQL:
CREATE TABLE,SELECT, andINSERT. - Familiarity with Delta tables in Databricks, including the idea that every write is a transaction recorded in a log.
- Access to a Databricks workspace with a SQL warehouse or cluster.
- A working idea of cloud object storage and of file formats such as CSV, JSON, and Parquet.
You do not need to know Auto Loader or Structured Streaming. The chapter introduces them only to show where they differ from COPY INTO.
What batch ingestion means
Ingestion is the step that moves data from where it is produced into tables you can query. Source systems often drop data as files in cloud object storage. Examples are a nightly CSV export from an order system, JSON events written by an application, or Parquet files from a partner.
Batch ingestion processes those files in discrete runs. A run starts, finds the files that are ready, loads them, and finishes. Nothing runs between runs. This contrasts with streaming ingestion, where a long-running process watches for new data and loads it continuously.
Batch is a good fit when:
- Data arrives on a known cadence, such as hourly or daily.
- Consumers do not need results within seconds.
- You want a run to have a clear start, end, and result you can check.
- The number of new files per run is modest.
Batch does not mean "large and slow". A batch run can load a few files in seconds. The word describes the execution model, which is bounded runs rather than a process that never ends.
Where COPY INTO fits
COPY INTO is a SQL command that loads files from a Unity Catalog volume or a cloud storage location into a Delta table. It reads the files, appends the rows to the table, and remembers which files it has loaded. On the next run it skips those files.
Databricks offers several ways to get file data into a table. They overlap, so it helps to know what separates them.
| Approach | Execution model | Tracks loaded files | Typical use |
|---|---|---|---|
| `COPY INTO` | Batch SQL command | Yes | Scheduled loads of new files into an existing table |
| Auto Loader | Incremental, streaming-style | Yes | Continuous or frequent ingestion, very large file counts |
| `CREATE TABLE AS SELECT` from files | One-off batch | No | Creating a table from files once |
| `INSERT INTO ... SELECT` from files | Batch | No | Ad hoc appends where you control duplicates yourself |
The deciding feature is file tracking. With a plain INSERT INTO ... SELECT over a folder, running the statement twice reads the same files twice and writes duplicate rows. COPY INTO avoids this without any bookkeeping from you. A later chapter covers how the tracking works and how to override it.
Auto Loader solves the same problem in a more scalable way. It discovers new files incrementally instead of listing the whole folder each time. A later chapter compares the two in detail. For now, the working rule is that COPY INTO suits sources with files in the thousands, run on a schedule, and Auto Loader suits sources with very large file counts or a need for continuous arrival handling.
How a COPY INTO run works
A run has four stages. Knowing them helps you diagnose problems later.
- List. The command lists the files in the source path, narrowed by any file selection you specify.
- Filter. It compares that list with the files already recorded as loaded for this target table. Files already loaded are dropped.
- Read and write. It reads the remaining files with the file format and options you gave, and writes the rows to the Delta table as one transaction.
- Record. It records the newly loaded files so the next run skips them.
Two consequences follow from this model.
First, the tracking belongs to the target table, not to the source folder or the statement text. If you load the same folder into two different tables, each table tracks its own files independently.
Second, the filter step compares file identity, which means the path and file metadata. It does not compare file contents. A file that was already loaded and later modified in place is not a good pattern to rely on. Write new files with new names.
A first look at the statement
The statement has three required parts: the target table, the source, and the file format. Here is a minimal load. The first statement creates the table, so the example runs on its own.
CREATE TABLE IF NOT EXISTS main.sales.orders (
order_id BIGINT,
amount DECIMAL(10, 2),
order_ts TIMESTAMP
);
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true');The pieces are:
COPY INTO main.sales.ordersnames the target Delta table with its three-level Unity Catalog name (catalog, schema, table).FROM '/Volumes/main/sales/landing/orders/'is the source folder. Here it is a Unity Catalog volume path.FILEFORMAT = CSVtells Databricks how to parse the files. Other supported formats include JSON, Parquet, Avro, ORC, text, and binary files.FORMAT_OPTIONSpasses format-specific settings. For CSV,'header' = 'true'treats the first row of each file as column names.
The command returns a one-row result. Its columns include num_affected_rows, num_inserted_rows, and num_skipped_rows. Run the statement a second time with no new files and the result shows zero rows inserted. That zero is the file tracking at work, and it is the quickest way to see the idempotency described above.
When COPY INTO is the right choice
Use it when most of these statements are true:
- Your source is files in a volume or cloud storage, and a SQL statement is enough to describe the load.
- Runs are scheduled, for example from a Databricks job, rather than continuous.
- The target is a Delta table that you append to.
- You want safe re-runs after a failure without writing deduplication logic.
- The number of files in the source folder stays manageable.
Look elsewhere when:
- Files arrive in very large numbers, or in a folder that accumulates millions of files. Listing the whole folder on each run becomes slow, and Auto Loader is designed for this.
- You need to react to data within seconds or minutes. A streaming approach fits better.
- You need to update or delete existing rows, not append.
COPY INTOonly appends. To merge changes you load into a staging table and then useMERGE INTO. - The source is a database or SaaS application, not files. A connector-based ingestion tool is a better fit than staging files yourself.
Common errors at this stage
These mistakes come from a wrong mental model, not from syntax, so they are worth naming early.
- Treating COPY INTO as an upsert. It appends. If a corrected file contains an order that was already loaded, you get a second row for it, not an update.
- Expecting a reload after editing a file. Because tracking is by file identity, a file that was already loaded is skipped even if you changed its contents. Giving the corrected data a new file name is the safe approach.
- Assuming the source folder drives idempotency. The record lives with the target table. Dropping and recreating the table resets it, so the next run loads everything again.
- Forgetting permissions. The user or job running the command needs access to the source location and to write to the target table. The next chapter covers this in detail.
- Choosing it for a streaming need. If someone asks for data "as soon as it lands", a scheduled batch command will not meet that without very frequent runs, and at that point Auto Loader is usually the better choice.
Exercise
A nightly job runs COPY INTO against a folder, and the folder receives 200 new CSV files each night. Yesterday an analyst edited one already-loaded file in place to fix a wrong amount, and today's run did not pick up the fix. Why, and what should the analyst do instead?
Show answer
COPY INTO tracks which files it has loaded by file identity, so it skips a file it has already loaded even if its contents changed. The edit is never read. The analyst should write the corrected data as a new file with a new name, which the next run treats as new. Because COPY INTO only appends, the old row also stays in the table, so the corrected row needs to be reconciled, for example by loading into a staging table and using MERGE INTO.
Exercise
You must load a partner's daily Parquet files into a Delta table and keep the table updated within one minute of each file arriving. Is a scheduled COPY INTO the best fit? Give one reason.
Show answer
No. COPY INTO is a batch command that runs only when it is triggered, so meeting a one-minute target means running it constantly, and each run lists the whole folder. Auto Loader discovers new files incrementally and handles continuous arrival, so it fits this requirement better.