適用於:
Azure Data Factory
Azure Synapse Analytics
秘訣
關於 Microsoft Fabric 中資料工廠的相當活動,請參見交換活動。
Switch 活動所提供的功能與 Switch 陳述式在程式設計語言中提供的功能相同。 它會評估一組活動,這組活動與符合條件評估的案例相對應。
使用 UI 建立 Switch 活動
若要在管線中使用 Switch 活動,請完成下列步驟:
- 在管道 [活動] 窗格中搜尋 Switch,然後將 Switch 活動新增至管線畫布。
- 在畫布上選取 Switch 活動 (若未選取),再選取其 [活動] 索引標籤以編輯詳細資料。
- 輸入要讓 Switch 評估的運算式。 這可以是運算式、函式、系統變數或其他活動的輸出的任意動態組合。
- 選取 [新增案例] 以新增更多案例。 如果沒有符合的案例,則會使用預設的案例活動。
- 輸入新案例的值。
- 選取 [編輯] 按鈕,新增將在運算式評估為相符案例時執行的活動。
JSON 語法
{
"name": "<Name of the activity>",
"type": "Switch",
"typeProperties": {
"expression": {
"value": "<expression that evaluates to some string value>",
"type": "Expression"
},
"cases": [
{
"value": "<string value that matches expression evaluation>",
"activities": [
{
"<Activity 1 definition>"
},
{
"<Activity 2 definition>"
},
{
"<Activity N definition>"
}
]
}
],
"defaultActivities": [
{
"<Activity 1 definition>"
},
{
"<Activity 2 definition>"
},
{
"<Activity N definition>"
}
]
}
}
類型屬性
| 屬性 | 描述 | 允許的值 | 必要 |
|---|---|---|---|
| 名稱 | Switch 活動的名稱。 | String | 是 |
| 型別 | 必須設定為 Switch*。 | String | 是 |
| 運算式 | 必須評估為字串值的運算式 | 具有結果類型字串的運算式 | 是 |
| 案例 | 一組案例,包含一個值,以及當該值符合運算式評估結果時要執行的一組活動。 必須至少提供一個案例。 上限為 25 個案例。 | 案例物件的陣列 | 是 |
| defaultActivities | 在未滿足運算式評估時執行的一組活動。 | 活動的陣列 | 是 |
範例
此範例中的管線會將資料從輸入資料夾複製到輸出資料夾。 管線參數 routeSelection 會決定輸出資料夾。
附註
本節提供 JSON 定義和 PowerShell 命令範例,以供執行管線。 若想了解如何使用Azure PowerShell和 JSON 定義建立資料工廠管線,請參考 tutorial: using Azure PowerShell 建立資料工廠。
具有 Switch 活動的管線 (Adfv2QuickStartPipeline.json)
{
"name": "Adfv2QuickStartPipeline",
"properties": {
"activities": [
{
"name": "MySwitch",
"type": "Switch",
"typeProperties": {
"expression": {
"value": "@pipeline().parameters.routeSelection",
"type": "Expression"
},
"cases": [
{
"value": "1",
"activities": [
{
"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"
}
}
}
]
},
{
"value": "2",
"activities": [
{
"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"
}
}
}
]
},
{
"value": "3",
"activities": [
{
"name": "CopyFromBlobToBlob3",
"type": "Copy",
"inputs": [
{
"referenceName": "BlobDataset",
"parameters": {
"path": "@pipeline().parameters.inputPath",
},
"type": "DatasetReference"
}
],
"outputs": [
{
"referenceName": "BlobDataset",
"parameters": {
"path": "@pipeline().parameters.outputPath3",
},
"type": "DatasetReference"
}
],
"typeProperties": {
"source": {
"type": "BlobSource"
},
"sink": {
"type": "BlobSink"
}
}
}
]
},
],
"defaultActivities": []
}
}
],
"parameters": {
"inputPath": {
"type": "String"
},
"outputPath1": {
"type": "String"
},
"outputPath2": {
"type": "String"
},
"outputPath3": {
"type": "String"
},
"routeSelection": {
"type": "String"
}
}
}
}
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/outputCase1",
"outputPath2": "adftutorial/outputCase2",
"outputPath2": "adftutorial/outputCase3",
"routeSelection": "1"
}
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"
相關內容
請參閱 Data Factory 支援的其他控制流程活動: