Sunday, October 11, 2026

How to load json files with different schema to azure sql db in Azure Data Factory ?

To implement dynamic schema mapping in Azure Data Factory (ADF) using an SQL metadata table and a stored procedure, you use a Lookup Activity to query your mapping rules and pass them dynamically into the Copy Activity's Translator property.

Here is how to set up and configure this pattern end-to-end.

Step 1: Create the Metadata Mapping Table

Create a table in your Azure SQL Database to store the mapping definitions between your source fields and target table columns.

SQL
CREATE TABLE dbo.PipelineColumnMappings (
    MappingId INT IDENTITY(1,1) PRIMARY KEY,
    PipelineName NVARCHAR(100),
    SourceColumn NVARCHAR(100),
    TargetColumn NVARCHAR(100),
    TargetTableName NVARCHAR(100)
);
Insert your mapping configurations for your pipeline:

SQL
INSERT INTO dbo.PipelineColumnMappings (PipelineName, SourceColumn, TargetColumn, TargetTableName)
VALUES 
('DynamicCopyPipeline', 'id', 'EmployeeID', 'Employees'),
('DynamicCopyPipeline', 'fullName', 'EmployeeName', 'Employees'),
('DynamicCopyPipeline', 'dept', 'Department', 'Employees');

Step 2: Create the Stored Procedure

Create a stored procedure that queries the mapping table and formats the output structure expected by ADF column mappings (source.name and sink.name).

SQL
CREATE PROCEDURE dbo.GetPipelineMappings
    @PipelineName NVARCHAR(100)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT 
        SourceColumn AS [source.name],
        TargetColumn AS [sink.name]
    FROM dbo.PipelineColumnMappings
    WHERE PipelineName = @PipelineName;
END

Step 3: Configure the ADF Pipeline

  1. Add a Lookup Activity:

    • Name it LookupMappings.

    • Set your Azure SQL Dataset as the source.

    • Select Stored procedure as the use query option and choose dbo.GetPipelineMappings.

    • Add the stored procedure parameter PipelineName with a value matching your pipeline name (e.g., DynamicCopyPipeline).

  2. Add a Copy Activity:

    • Set up your Source dataset (e.g., your JSON file or blob storage) and Sink dataset (your target Azure SQL table).

    • In the ADF UI, navigate to the Mapping tab. By default, the UI expects static mappings. To make it dynamic, you can switch to the pipeline Code view ({} icon at the top right of the pipeline canvas).

  3. Inject Dynamic Mappings in Pipeline JSON:
    Locate your Copy Activity block in the JSON code and update the translator property to accept the array output from your Lookup Activity:

    JSON
    {
        "name": "CopyDataWithDynamicMapping",
        "type": "Copy",
        "dependsOn": [
            {
                "activity": "LookupMappings",
                "dependencyConditions": ["Succeeded"]
            }
        ],
        "typeProperties": {
            "source": {
                "type": "JsonSource",
                "storeSettings": {
                    "type": "AzureBlobStorageReadSettings",
                    "recursive": true
                }
            },
            "sink": {
                "type": "SqlSink",
                "writeBatchSize": 10000
            },
            "translator": {
                "type": "TabularTranslator",
                "mappings": "@activity('LookupMappings').output.value"
            }
        },
        "inputs": [
            {
                "referenceName": "JsonSourceDataset",
                "type": "DatasetReference"
            }
        ],
        "outputs": [
            {
                "referenceName": "SqlSinkDataset",
                "type": "DatasetReference"
            }
        ]
    }
    
When the pipeline runs, the Lookup Activity executes the stored procedure and returns an array of source-to-target column mappings. The Copy Activity's TabularTranslator evaluates this array dynamically at runtime, allowing you to alter mappings simply by updating rows in your Azure SQL mapping table without modifying the ADF pipeline structure.

No comments:

Post a Comment

pandas vs pyspark dataframe

  📊 Pandas vs Spark DataFrame Aspect Pandas Spark DataFrame (PySpark) Execution Model Eager (operations run immediately) Lazy (builds DAG, ...