Share via

datafactory copy activity with dynamic mapping

Jorge García 0 Reputation points
2024-11-01T16:55:39.2366667+00:00

Hello,

I am trying to modify my JSON pipeline file to use a translator instead of static mapping ...

When I statically define the values like:

translator": {
                        "type": "TabularTranslator",
                        "mappings": [
                            {
                                "source": {
                                    "name": "variable1",
                                    "type": "Decimal"
                                },
                                "sink": {
                                    "name": "v1",
                                    "type": "Decimal"
                                }
                            },
                            {
                                "source": {
                                    "name": "variable2",
                                    "type": "Guid"
                                },
                                "sink": {
                                    "name": "v2",
                                    "type": "Guid"
                                }
                            },
}

I can read my parquet file with exactly 2 columns.

Unfortunately, I cant find the way to do it dynamically. If I do something like this, I return all the source columns when reading the dataframe and the column names dont change. Basically, the dynamically defined mapping is ignored by ADF.

"enableStaging": false,
"translator": {
    "value": "@variables('final_mapping')",
    "type": "Expression"
}


"variables": {

            "final_mapping": {
                "type": "Array",
                "defaultValue": [
                    {
                        "source": {
                            "name": "variable1",
                            "type": "Decimal"
                        },
                        "sink": {
                            "name": "v1",
                            "type": "Decimal"
                        }
                    },
                    {
                        "source": {
                            "name": "variable2",
                            "type": "Guid"
                        },
                        "sink": {
                            "name": "v2",
                            "type": "Guid"
                        }
                    }
                ]
            }
        },

Can somebody help me? thanks in advance.

Azure Data Factory
Azure Data Factory

An Azure service for ingesting, preparing, and transforming data at scale.

0 comments No comments

3 answers

Sort by: Most helpful
  1. AnnuKumari-MSFT 34,571 Reputation points Microsoft Employee Moderator
    2024-11-04T06:24:34.49+00:00

    Hi Jorge García ,

    Welcome to Microsoft Q&A platform and thanks for posting your query here.

    As per my understanding , you were facing issue while trying to perform dynamic mapping in ADF.

    Glad that you figured out the way to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

    According to Microsoft Docs (https://learn.microsoft.com/en-us/azure/data-factory/copy-activity-schema-and-type-mapping#default-mapping) it is necessary to create the finalMapping variable as a JSON

    {"type":"TabularTranslator","mappings":[{"source":{"name":"Id"},"sink":{"name":"CustomerID"}},{"source":{"name":"Name"},"sink":{"name":"LastName"}},{"source":{"name":"LastModifiedDate"},"sink":{"name":"ModifiedDate"}}]}
    
    

    So after storing the mappings array in a variable, creating a JSON file like above resolved the issue.

    More details: Dynamic Column mapping in Copy Activity in Azure Data Factory

    Kindly accept the answer by clicking on Accept answer button so that the solution reaches the community in case anyone is facing similar issue. Thankyou.

    Was this answer helpful?


  2. Jorge García 0 Reputation points
    2024-11-03T09:43:22.27+00:00

    Finally I managed to find the solution:

    According to Microsoft Docs (https://learn.microsoft.com/en-us/azure/data-factory/copy-activity-schema-and-type-mapping#default-mapping) it is necessary to create the finalMapping variable as a JSON like the following:

    {"type":"TabularTranslator","mappings":[{"source":{"name":"Id"},"sink":{"name":"CustomerID"}},{"source":{"name":"Name"},"sink":{"name":"LastName"}},{"source":{"name":"LastModifiedDate"},"sink":{"name":"ModifiedDate"}}]}

    So after storing my mappings array in a variable, I also had to create a JSON file like the one I pasted.

    Kind regards.

    Was this answer helpful?

    0 comments No comments

  3. Amira Bedhiafi 42,941 Reputation points MVP Volunteer Moderator
    2024-11-02T13:46:48.88+00:00

    You need to use a Set Variable activity before the Copy Data activity to assign a value to the final_mapping variable dynamically.

    For the Copy Data activity, it should references th final_mapping variable correctly as an expression. The syntax for referencing variables dynamically in ADF requires the use of @{}.

    And then update the translator section in the Copy Data activity to reference the variable directly.

    Your json should look like below :

    
    "activities": [
    
        {
    
            "name": "SetFinalMapping",
    
            "type": "SetVariable",
    
            "typeProperties": {
    
                "variableName": "final_mapping",
    
                "value": {
    
                    "type": "Array",
    
                    "value": [
    
                        {
    
                            "source": {
    
                                "name": "variable1",
    
                                "type": "Decimal"
    
                            },
    
                            "sink": {
    
                                "name": "v1",
    
                                "type": "Decimal"
    
                            }
    
                        },
    
                        {
    
                            "source": {
    
                                "name": "variable2",
    
                                "type": "Guid"
    
                            },
    
                            "sink": {
    
                                "name": "v2",
    
                                "type": "Guid"
    
                            }
    
                        }
    
                    ]
    
                }
    
            }
    
        },
    
        {
    
            "name": "CopyDataWithDynamicMapping",
    
            "type": "Copy",
    
            "typeProperties": {
    
                "source": {
    
                    "type": "ParquetSource"
    
                },
    
                "sink": {
    
                    "type": "YourSinkType"
    
                },
    
                "enableStaging": false,
    
                "translator": {
    
                    "type": "TabularTranslator",
    
                    "mappings": {
    
                        "value": "@variables('final_mapping')",
    
                        "type": "Expression"
    
                    }
    
                }
    
            }
    
        }
    
    ]
    
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.