
Azure Data Factory Cookbook
By :

In this recipe, we introduce you to the Filter and ForEach activities. We shall enhance the pipeline from the previous recipe to not just examine the data in the Azure Storage container, but filter it based on the file type and then record the last-modified date for every .csv
file in the folder.
The preparation steps are the same as for the previous recipe. We shall be reusing the pipeline from the Using Metadata and Stored Procedure activities recipe, so if you did not go through the steps then, do so now.
pl_orchestration_recipe_3
.CsvDataFolder
is selected as the dataset.FilterOnCsv
.@activity('CsvDataFolder Metadata').output.childItems
@endswith(item().name, '.csv')
Figure 2.19: Pipeline status overview in Debug mode
After the pipeline is finished running, hover over the row representing the Get Metadata activity run in the Output pane and examine the activity’s output. You should see that the Get Metadata activity fetched the metadata for all the files in the folder, as follows:
Figure 2.20: Get Metadata activity output
Do the same for the FilterOnCSV activity and verify that the outputs were filtered to only the csv
files.
@activity('FilterOnCSV').output.Value
.ForEach Metadata
.CsvData
(the parameterized dataset we created in the Using parameters and built-in functions recipe) as the dataset for this activity. If you do not have this dataset, please refer to the Using parameters and built-in functions recipe to see how to create a parameterized dataset.filename
parameter, enter @item().name
.Figure 2.21: Adding arguments in the Field list section
[dbo][InsertFileMetadata]
as the stored procedure name. @{item().name}
@convertFromUtc(activity('ForEach Metadata').output.lastModified,'Pacific Standard Time')
@convertFromUtc(utcnow(), 'Pacific Standard Time')
(you can use your own time zone here, as well):Figure 2.22: Stored Procedure activity configuration
Run your whole pipeline in Debug mode. When it is finished, you should see two additional rows in your FileMetadata table (in Azure SQL Database) showing the last-modified date for airlines.csv
and countries.csv.
In this recipe, we used the Metadata activity again and took advantage of the childItems
option to retrieve information about the folder. After this, we filtered the output to restrict processing to CSV files only with the help of the Filter activity.
Next, we needed to select only the CSV files from the folder for further processing. For this, we added a Filter activity. Using @activity('Get Metadata').output.childItems
, we specified that the Filter activity’s input is the metadata of all the files inside the folder. We configured the Filter activity’s condition to only keep files whose name ends with csv
(the built-in endswith
function gave us a convenient way to do this).
Finally, in order to process each file separately, we used the ForEach activity, which we used in step 6. ForEach is what is called a compound activity, because it contains a group of activities that are performed on each of the items in a loop. We configured the Filter activity to take as input the filtered file list (the output of the Filter activity), and in steps 7 and 8, we designed the sequence of actions that we want to have performed on each of the files. We used a second instance of the Metadata activity for this sub-pipeline and configured it to retrieve information about a particular file. To accomplish this, we configured it with the parameterized CsvData
dataset and specified the filename. In order to refer to the file, we used the built-in formula @item
(which provides a reference to the current file in the ForEach
loop) and indicated that we need the name
property of that object.
The configuration of the Stored Procedure activity is similar to the previous step. In order to provide the filename for the Stored Procedure parameters, we again referred to the provided current object reference, @item
. We could also have used @activity('ForEach Metadata').output.itemName
, as we did in the previous recipe.
Change the font size
Change margin width
Change background colour