Copy every table with one Data Factory pipeline

A metadata-driven pipeline in Azure Data Factory reads a control table and copies every table in it, so a new table is a new row, not a new pipeline.

Short answer: keep a control table with one row per source table, and build one parameterised pipeline: a Lookup reads the control table, a ForEach runs one Copy per row, and each copy writes its result back. Adding a table to the load is then an INSERT, not a new pipeline.

A pipeline per table is fine for three tables. At thirty, every change to how you copy (a new folder layout, a retry setting, a log line) has to be made thirty times, and every new table is a release. The metadata-driven pattern turns that around: the pipeline stays the same, and what it copies lives in a table you can read, query and change.

How does a metadata-driven pipeline work?

  1. 1
    Control tableone row per source table, with its key and its last load
  2. 2
    Lookupreads the active rows
  3. 3
    ForEachone iteration per row, several at a time
  4. 4
    Copythe table into its own folder in the lake
  5. 5
    Write backthe run's result into the control table
One pipeline, any number of tables

The pipeline never names a table. The Lookup returns a list, the ForEach walks it, and inside the loop @item() is the current row: its schema, its table name and whatever else the row holds.

What goes in the control table?

Only what the pipeline needs to decide what to copy and to report what happened:

SchemaNameTableNameWatermarkColumnIsActiveLastRunStatus
SalesLTCustomerModifiedDate1Succeeded
SalesLTProductModifiedDate1Succeeded
SalesLTSalesOrderHeaderModifiedDate1Failed
SalesLTProductModelModifiedDate0Skipped
The control table the Lookup reads (etl.LoadControl)

IsActive switches a table off without deleting its row, and the last two columns tell you, and your monitoring, what the last run did. The struck-out row above is switched off, so the Lookup never returns it.

CREATE TABLE etl.LoadControl (
    SchemaName      sysname      NOT NULL,
    TableName       sysname      NOT NULL,
    WatermarkColumn sysname      NOT NULL,
    LastWatermark   datetime2    NOT NULL DEFAULT '1900-01-01',
    IsActive        bit          NOT NULL DEFAULT 1,
    LastRunAt       datetime2    NULL,
    LastRunStatus   nvarchar(20) NULL,
    CONSTRAINT PK_LoadControl PRIMARY KEY (SchemaName, TableName)
);

How do you build the pipeline?

  1. A Lookup activity with First row only turned off, and the query below as its source.
  2. A ForEach whose items are @activity('Lookup control').output.value. Leave Sequential off and set a Batch count, so several tables copy at once.
  3. Inside the ForEach, a Copy activity. Its source dataset takes SchemaName and TableName as parameters, filled with @item().SchemaName and @item().TableName. Its sink dataset writes to a folder built from the same values, for example raw/@{item().SchemaName}/@{item().TableName}/.
  4. After the Copy, a Stored procedure activity that writes LastRunAt and LastRunStatus back for that row. Put one on the success path and one on the failure path, so a failed table is marked failed.

The Lookup's query:

SELECT SchemaName, TableName, WatermarkColumn, LastWatermark
FROM etl.LoadControl
WHERE IsActive = 1;

Two datasets (one source, one sink) serve every table. That is the whole saving: the copy logic exists once.

What are the limits?

  • A Lookup returns at most 5,000 rows and 4 MB. A control table rarely gets near that, but if yours does, split it by a group column and run one ForEach per group.
  • A ForEach runs up to 50 iterations at the same time (the default is 20). The source database decides the real limit: start low and raise it while you watch it.
  • A ForEach cannot hold another ForEach. If each table needs its own loop, move the inner part into a second pipeline and call it with Execute Pipeline.

What goes wrong?

  • One table fails, and you only find out from the pipeline's red status. The write-back step on the failure path is what tells you which table, and why. Without it you open run history and click through the iterations.
  • A copy runs into a table that changed shape. A new column in the source flows into the Parquet files without complaint, but a report downstream may break. Keep schema drift visible: compare the columns the copy wrote with the columns it wrote last time.
  • Every run copies everything. That is the next step: a watermark per table, so each run asks for the newer rows only. See Load only the rows that changed.

Where can you check this?

Where do you start?

The open course Your first metadata-driven pipeline builds this pattern step by step in your own Azure subscription: the control table, the Lookup, ForEach and Copy, and the watermark that makes it incremental. The pattern sits with the others on the map of my work.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.