简体   繁体   中英

matching the columns in a source file with sink table columns to make sure they match using Azure Data Factory

I have an Azure Data factory trigger that is fired off when a file is placed in blob storage, this trigger will start pipeline execution and pass the file name to the data flow activity. I would like to make sure that all the column names from the header row in the file are in the sink table. There is an identity column in the sink table that should not be in the comparison. Not sure how to tackle this task, I've read about the 'derived column' activity, is that the route I should take?

Strategy:

Use two ADF pipelines, one to get a list of all files and another one to process each file copying its content to a specific SQL table.

Setup:

I've created 4 CSV files, following the pattern you need: “[CustomerID] [TableName] [FileID].csv” and 4 SQL tables, one for each type of file.

  • A_inventory_0001.csv: inventory records for customer A, to be inserted into the SQL table “A_Inventory”.
  • A_sales_0003.csv: sales records for customer A, to be inserted into the SQL table “A_Sales”.
  • B_inventory_0002.csv: inventory records for customer B, to be inserted into the SQL table “B_Inventory”.
  • B_sales_0004.csv: sales records for customer B, to be inserted into the SQL table “B_Sales”

战略

Linked Services

In Azure Data Factory, the following linked services were create using Key Vault (Key Vault is optional).

链接服务

Datasets

The following datasets were created. Note we have created some parameters to allow the pipeline to specify the source file and the destination SQL table.

The dataset “AzureSQLTable” has a parameter to specify the name of the destination SQL table.

数据集-AzureSQLTable

The dataset “DelimitedTextFile” has a parameter to specify the name of the source CSV file.

数据集分隔文本文件

The dataset “DelimitedTextFiles” has no parameter because it will be used to list all files from source folder.

数据集分隔文本文件

Pipelines

The first pipeline “Get Files” will get the list of CSV files from source folder (Get Metadata activity), and then, for each file, call the second pipeline passing the CSV file name as a parameter.

管道-GetFiles1

管道-GetFiles2

Inside the foreach loop, there is a call to the second pipeline “Process File” passing the file name as a parameter.

管道-GetFiles3

The second pipeline has a parameter “pFileName” to receive the name of the file to be processed and a variable to calculate the name of the destination table based on the file name.

管道-ProcessFile1

The first activity is to use a split in the file name to extract the parts we need to compose the destination table name. In the expression bellow we are splitting the file name using the “__” separator and then using the first and second parts to compose the destination table name. @concat(string(split(pipeline().parameters.pFileName, '_')[0]),'_',string(split(pipeline().parameters.pFileName, '_')[10]))

管道-ProcessFile2

The second activity will then copy the file from the source “pFileName” to the desnation table “vTableName” using dynamic mapping, ie not adding specific column names as this will be dynamic.

管道-ProcessFileCopy

The files I used in this example and the ADF code are available here: https://github.com/diegoeick/stack-overflow/tree/main/69340699

I hope this will resolve your issue.

In case you still need to save the CustomerID and FileID in the database tables, you can use the dynamic mapping and use the available parameters (filename) and create a json with the dynamic mapping in the mapping tab of your copy activity. You can find more details here: https://learn.microsoft.com/en-us/azure/data-factory/copy-activity-schema-and-type-mapping#parameterize-mapping

You can select or filter which columns reside in sink dataset or table by using " Field mapping ". You can optionally use " derived columns " transformation, however in the " sink transformation " you will have this by default and is set to " Auto mapping ". Here you can add or remove which columns are written to sink.

In the below example the column " id " can be assumed as similar to " Identity " column in your table. Assuming all the files have same columns:

在此处输入图像描述

在此处输入图像描述

Once you have modified as per your need, you can confirm the same from the " inspect " tab before run.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM