適用於:
Azure Data Factory
Azure Synapse Analytics
秘訣
關於 Microsoft Fabric 中 Data Factory 中等效的活動,請參見 If Condition 活動。
If 條件活動所提供的功能,與 If 陳述式在程式設計語言中提供的功能相同。 它能在條件評估為 true 時執行一組活動,並在條件評估為 false 時執行另一組活動。
使用 UI 建立 If 條件活動
若要在管線中使用 If 條件活動,請完成下列步驟:
在管線 [活動] 窗格中搜尋 If,然後將 If 條件活動拖曳至管線畫布。
在畫布上選取新的 If 條件活動 (若未選取),再選取其 [活動] 索引標籤以編輯其詳細資料。
選取 If 條件 [活動] 索引標籤上的 [編輯活動] 按鈕,或直接從管線畫布上的 If 條件,新增運算式評估為
true或false時會執行的活動。
語法
{
"name": "<Name of the activity>",
"type": "IfCondition",
"typeProperties": {
"expression": {
"value": "<expression that evaluates to true or false>",
"type": "Expression"
},
"ifTrueActivities": [
{
"<Activity 1 definition>"
},
{
"<Activity 2 definition>"
},
{
"<Activity N definition>"
}
],
"ifFalseActivities": [
{
"<Activity 1 definition>"
},
{
"<Activity 2 definition>"
},
{
"<Activity N definition>"
}
]
}
}
類型屬性
| 屬性 | 描述 | 允許的值 | 必要 |
|---|---|---|---|
| 名稱 | If 條件活動的名稱。 | String | 是 |
| 型別 | 必須設定為 IfCondition | String | 是 |
| 運算式 | 必須評估為 true 或 false 的運算式 | 結果類型為布林的運算式 | 是 |
| ifTrueActivities | 會在運算式評估為 true 時執行的一組活動。 |
Array | 是 |
| ifFalseActivities | 會在運算式評估為 false 時執行的一組活動。 |
Array | 是 |
範例
此範例中的管線會將資料從輸入資料夾複製到輸出資料夾。 輸出資料夾是由管線參數:routeSelection 的值所決定。 如果 routeSelection 的值為 true,資料將會被複製到 outputPath1。 而且,如果 routeSelection 的值為 false,該資料將會被複製到 outputPath2。
附註
本節提供 JSON 定義和 PowerShell 命令範例,以供執行管線。 有關使用Azure PowerShell和 JSON 定義建立管線的逐步教學,請參見 tutorial: create a data factory by using Azure PowerShell。
具有 If 條件活動的管線 (Adfv2QuickStartPipeline.json)
{
"name": "Adfv2QuickStartPipeline",
"properties": {
"activities": [
{
"name": "MyIfCondition",
"type": "IfCondition",
"typeProperties": {
"expression": {
"value": "@bool(pipeline().parameters.routeSelection)",
"type": "Expression"
},
"ifTrueActivities": [
{
"name": "CopyFromBlobToBlob1",
"type": "Copy",
"inputs": [
{
"referenceName": "BlobDataset",
"parameters": {
"path": "@pipeline().parameters.inputPath"
},
"type": "DatasetReference"
}
],
"outputs": [
{
"referenceName": "BlobDataset",
"parameters": {
"path": "@pipeline().parameters.outputPath1"
},
"type": "DatasetReference"
}
],
"typeProperties": {
"source": {
"type": "BlobSource"
},
"sink": {
"type": "BlobSink"
}
}
}
],
"ifFalseActivities": [
{
"name": "CopyFromBlobToBlob2",
"type": "Copy",
"inputs": [
{
"referenceName": "BlobDataset",
"parameters": {
"path": "@pipeline().parameters.inputPath"
},
"type": "DatasetReference"
}
],
"outputs": [
{
"referenceName": "BlobDataset",
"parameters": {
"path": "@pipeline().parameters.outputPath2"
},
"type": "DatasetReference"
}
],
"typeProperties": {
"source": {
"type": "BlobSource"
},
"sink": {
"type": "BlobSink"
}
}
}
]
}
}
],
"parameters": {
"inputPath": {
"type": "String"
},
"outputPath1": {
"type": "String"
},
"outputPath2": {
"type": "String"
},
"routeSelection": {
"type": "String"
}
}
}
}
另一個運算式的範例為:
"expression": {
"value": "@equals(pipeline().parameters.routeSelection,1)",
"type": "Expression"
}
Azure 儲存體 連結服務(AzureStorageLinkedService.json)
{
"name": "AzureStorageLinkedService",
"properties": {
"type": "AzureStorage",
"typeProperties": {
"connectionString": "DefaultEndpointsProtocol=https;AccountName=<Azure Storage account name>;AccountKey=<Azure Storage account key>"
}
}
}
參數化Azure Blob 資料集(BlobDataset.json)
管線會將 folderPath 設定為管線的 outputPath1 或 outputPath2 參數。
{
"name": "BlobDataset",
"properties": {
"type": "AzureBlob",
"typeProperties": {
"folderPath": {
"value": "@{dataset().path}",
"type": "Expression"
}
},
"linkedServiceName": {
"referenceName": "AzureStorageLinkedService",
"type": "LinkedServiceReference"
},
"parameters": {
"path": {
"type": "String"
}
}
}
}
管線參數 JSON (PipelineParameters.json)
{
"inputPath": "adftutorial/input",
"outputPath1": "adftutorial/outputIf",
"outputPath2": "adftutorial/outputElse",
"routeSelection": "false"
}
PowerShell 命令
附註
我們建議你使用 Azure Az PowerShell 模組來與 Azure 互動。 要開始,請參考 安裝 Azure PowerShell。 想了解如何遷移到 Az PowerShell 模組,請參考 Migrate Azure PowerShell from AzureRM to Az。
這些命令假設您已將 JSON 檔案儲存至資料夾:C:\ADF。
Connect-AzAccount
Select-AzSubscription "<Your subscription name>"
$resourceGroupName = "<Resource Group Name>"
$dataFactoryName = "<Data Factory Name. Must be globally unique>";
Remove-AzDataFactoryV2 $dataFactoryName -ResourceGroupName $resourceGroupName -force
Set-AzDataFactoryV2 -ResourceGroupName $resourceGroupName -Location "East US" -Name $dataFactoryName
Set-AzDataFactoryV2LinkedService -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -Name "AzureStorageLinkedService" -DefinitionFile "C:\ADF\AzureStorageLinkedService.json"
Set-AzDataFactoryV2Dataset -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -Name "BlobDataset" -DefinitionFile "C:\ADF\BlobDataset.json"
Set-AzDataFactoryV2Pipeline -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -Name "Adfv2QuickStartPipeline" -DefinitionFile "C:\ADF\Adfv2QuickStartPipeline.json"
$runId = Invoke-AzDataFactoryV2Pipeline -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -PipelineName "Adfv2QuickStartPipeline" -ParameterFile C:\ADF\PipelineParameters.json
while ($True) {
$run = Get-AzDataFactoryV2PipelineRun -ResourceGroupName $resourceGroupName -DataFactoryName $DataFactoryName -PipelineRunId $runId
if ($run) {
if ($run.Status -ne 'InProgress') {
Write-Host "Pipeline run finished. The status is: " $run.Status -foregroundcolor "Yellow"
$run
break
}
Write-Host "Pipeline is running...status: InProgress" -foregroundcolor "Yellow"
}
Start-Sleep -Seconds 30
}
Write-Host "Activity run details:" -foregroundcolor "Yellow"
$result = Get-AzDataFactoryV2ActivityRun -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -PipelineRunId $runId -RunStartedAfter (Get-Date).AddMinutes(-30) -RunStartedBefore (Get-Date).AddMinutes(30)
$result
Write-Host "Activity 'Output' section:" -foregroundcolor "Yellow"
$result.Output -join "`r`n"
Write-Host "\nActivity 'Error' section:" -foregroundcolor "Yellow"
$result.Error -join "`r`n"
相關內容
查看其他支援的控制流程活動: