Sub topic 1
What batch ingestion is and where COPY INTO fits
Prerequisites
This chapter assumes you can do the following:
- Write basic SQL (CREATE TABLE, SELECT, INSERT).
- Work with Delta tables in Databricks.
- Open a Databricks workspace with a SQL warehouse or cluster.
- Recognize cloud object storage and file formats such as CSV, JSON and Parquet.
What batch ingestion is
Ingestion is the step that moves data from where it is produced into a table you can query. In batch ingestion, you load data in groups at a point in time. For example, a nightly job picks up all the files that arrived during the day and loads them together.
Batch ingestion is a good fit when:
- Data arrives as files in cloud storage, such as CSV exports from another system.
- Freshness of minutes or hours is enough.
- You want a simple job that starts, loads and finishes.
Streaming ingestion is different. It keeps running and processes data continuously as it arrives. This tutorial covers the batch case.
What COPY INTO is
COPY INTO is a SQL command that loads files from cloud storage or a Unity Catalog volume into a Delta table. Unity Catalog is the Databricks governance layer, and a volume is a governed location for files inside it.
COPY INTO is idempotent for files. Idempotent means that running the command again gives the same end result as running it once. COPY INTO records which files it has loaded into a table and skips them on later runs. So you can run the same statement every hour, and each run loads only the files that are new since the last one.
The shape of the command looks like this:
COPY INTO main.sales.orders
FROM '/Volumes/main/sales/landing/orders/'
FILEFORMAT = CSV
FORMAT_OPTIONS ('header' = 'true');You name the target table, the source location, the file format, and any options. The following chapters explain each part.
How a load runs
When you run the command, Databricks lists the files in the source location. It compares the list with the files already loaded into the target table and skips the ones it has seen. It then reads the new files and appends their rows to the Delta table.
Where COPY INTO fits
Databricks offers several ways to get files into tables. COPY INTO sits at the simple end of the range.
| Approach | Style | Typical use |
|---|---|---|
| COPY INTO | SQL command, run on demand or on a schedule | Batch loads from a modest number of files, SQL-first teams |
| Auto Loader | Incremental file discovery, usually in a pipeline | Very large or fast-growing file sets, continuous or frequent arrival |
| INSERT INTO ... SELECT | Query over data already in a table or readable as a table | Moving data you can already query, not tracking new files |
Pick COPY INTO when you want a short SQL statement, the file volume is moderate, and a scheduled run is fast enough. Chapter 12 compares it with Auto Loader in detail.
Common mistakes at this stage
- Treating it as a replace operation. COPY INTO appends. It does not overwrite existing rows.
- Expecting a change to an old file to be picked up. The file is tracked as loaded, so COPY INTO skips it on later runs.
- Assuming it deduplicates rows. It skips files, not rows. Duplicate rows inside new files still load.
Exercises
Exercise
A team receives CSV files in a volume throughout the day and runs the same COPY INTO statement every hour. Why does the table not fill up with duplicate rows, and what kind of duplicate can still happen?
Show answer
COPY INTO records each file it loads and skips those files on later runs, so each file is loaded once. Duplicates can still happen when the same row appears in two different files, because COPY INTO skips files, not rows.
Exercise
Name two conditions under which COPY INTO is a better choice than Auto Loader.
Show answer
Any two of these: the load is a batch that runs on demand or on a schedule, the number of files is moderate, and the team prefers a simple SQL statement over setting up a pipeline.