Azure Data Factory と Synapse Analytics での Until アクティビティ

適用対象: Azure Data Factory Azure Synapse Analytics

ヒント

企業向けのオールインワン分析ソリューション、Microsoft Fabric の Data Factory をお試しください。 Microsoft Fabric は、データ移動からデータ サイエンス、リアルタイム分析、ビジネス インテリジェンス、レポートまで、あらゆるものをカバーしています。 無料で新しい試用版を開始する方法について説明します。

Until アクティビティは、プログラミング言語における do-until ループ構文と同じ働きを持ちます。 Until アクティビティでは、そこに関連付けられている条件が true に評価されるまで、一連のアクティビティがループ実行されます。 内部アクティビティが失敗した場合、Until アクティビティは停止しません。 Until アクティビティにはタイムアウト値を指定できます。

UI を使用して Until アクティビティを作成する

パイプライン内で Until アクティビティを使用するには、次の手順を実行します。

  1. パイプラインのアクティビティ ウィンドウ内で Until を検索し、Until アクティビティをパイプライン キャンバスにドラッグします。

  2. キャンバス上で Until アクティビティを選択し (まだ選択されていない場合)、その [設定] タブを選択して、詳細を編集します。

    Shows the Settings tab of the Until activity in the pipeline canvas.

  3. Until アクティビティ内のすべての定義済み子アクティビティが実行された後に評価される式を入力します。 式が false に評価された場合、そのすべての子アクティビティが Until アクティビティによって再実行されます。 true に評価されると、Until アクティビティが完了します。 式には、リテラル文字列式、または動的な式、関数システム変数、または他のアクティビティからの出力の任意の組み合わせを使用できます。 次の例は、TestVariable という定義済みパイプライン配列変数の値をチェックして、['done'] に評価されるかどうかを確認しています。

    Shows the  Add dynamic content  pane with an expression to check a variable for a defined value.

  4. Until アクティビティによって実行されるアクティビティを定義します。それには、Until アクティビティ上の [アクティビティの編集] ボタンを直接選択するか、[アクティビティ] タブを選択して、そこでそのアクティビティを選択します。 新しいアクティビティ エディター ウィンドウが表示され、Until アクティビティを実行するためのアクティビティを追加できます。 この例では、Set Variable アクティビティが前述の式で参照される変数の値を ['done'] に設定するだけであるため、Until アクティビティの式は最初に実行されたときに true になり、Until アクティビティは停止します。 実際に使用するときは、必要な条件をチェックでき、Until アクティビティは、その条件が満たされるまで、式が評価されるたびに子アクティビティを実行し続けます。

    Shows the activities editor for an Until activity with a Set Variable activity defined.

構文

{
    "type": "Until",
    "typeProperties": {
        "expression":  {
            "value":  "<expression that evaluates to true or false>", 
            "type": "Expression"
        },
        "timeout": "<time out for the loop. for example: 00:10:00 (10 minute)>",
        "activities": [
            {
                "<Activity 1 definition>"
            },
            {
                "<Activity 2 definition>"
            },
            {
                "<Activity N definition>"
            }
        ]
    },
    "name": "MyUntilActivity"
}

型のプロパティ

プロパティ 説明 使用できる値 必須
name Until アクティビティの名前。 String はい
type Until に設定する必要があります。 String はい
expression true または false に評価される式であることが必要です 式。 はい
timeout ここに指定した時間が経過すると、do-until ループがタイムアウトします。 文字列。 d.hh:mm:ss または hh:mm:ss。 既定値は 7 日間です。 最大値は 90 日間です。 いいえ
Activities 式が true に評価されるまで実行される一連のアクティビティです。 アクティビティの配列。 はい

例 1

注意

このセクションでは、パイプラインを実行するための JSON の定義とサンプル PowerShell コマンドを紹介しています。 Azure PowerShell と JSON 定義を使用してパイプラインを作成するための詳細な手順が記載されているチュートリアルについては、Azure PowerShell を使用したデータ ファクトリの作成に関するチュートリアルをご覧ください。

Until アクティビティのあるパイプライン

この例では、パイプラインに Until および Wait という 2 つのアクティビティが含まれています。 Web アクティビティをループ実行する前に、指定した待ち時間が Wait アクティビティによって確保されます。 式と関数については、式言語と関数に関するページを参照してください。

{
    "name": "DoUntilPipeline",
    "properties": {
        "activities": [
            {
                "type": "Until",
                "typeProperties": {
                    "expression": {
                        "value": "@equals('Failed', coalesce(body('MyUnauthenticatedActivity')?.status, actions('MyUnauthenticatedActivity')?.status, 'null'))",
                        "type": "Expression"
                    },
                    "timeout": "00:10:00",
                    "activities": [
                        {
                            "name": "MyUnauthenticatedActivity",
                            "type": "WebActivity",
                            "typeProperties": {
                                "method": "get",
                                "url": "https://www.fake.com/",
                                "headers": {
                                    "Content-Type": "application/json"
                                }
                            },
                            "dependsOn": [
                                {
                                    "activity": "MyWaitActivity",
                                    "dependencyConditions": [ "Succeeded" ]
                                }
                            ]
                        },
                        {
                            "type": "Wait",
                            "typeProperties": {
                                "waitTimeInSeconds": 1
                            },
                            "name": "MyWaitActivity"
                        }
                    ]
                },
                "name": "MyUntilActivity"
            }
        ]
    }
}

例 2

このサンプルのパイプラインでは、ループで入力フォルダーから出力フォルダーにデータをコピーします。 repeat パラメーターの値が false に設定されるか、1 分経過してタイムアウトすると、ループが強制的に終了されます。

Until アクティビティのあるパイプライン (Adfv2QuickStartPipeline.json)

{
    "name": "Adfv2QuickStartPipeline",
    "properties": {
        "activities": [
            {
                "type": "Until",
                "typeProperties": {
                    "expression":  {
                        "value":  "@equals('false', pipeline().parameters.repeat)", 
                        "type": "Expression"
                    },
                    "timeout": "00:10:00",
                    "activities": [
                        {
                            "name": "CopyFromBlobToBlob",
                            "type": "Copy",
                            "inputs": [
                                {
                                    "referenceName": "BlobDataset",
                                    "parameters": {
                                        "path": "@pipeline().parameters.inputPath"
                                    },
                                    "type": "DatasetReference"
                                }
                            ],
                            "outputs": [
                                {
                                    "referenceName": "BlobDataset",
                                    "parameters": {
                                        "path": "@pipeline().parameters.outputPath"
                                    },
                                    "type": "DatasetReference"
                                }
                            ],
                            "typeProperties": {
                                "source": {
                                    "type": "BlobSource"
                                },
                                "sink": {
                                    "type": "BlobSink"
                                }
                            },
                            "policy": {
                                "retry": 1,
                                "timeout": "00:10:00",
                                "retryIntervalInSeconds": 60
                            }
                        }
                    ]
                },
                "name": "MyUntilActivity"
            }
        ],
        "parameters": {
            "inputPath": {
                "type": "String"
            },
            "outputPath": {
                "type": "String"
            },
            "repeat": {
                "type": "String"
            }                        
        }        
    }
}

Azure Storage のリンクされたサービス (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",
    "outputPath": "adftutorial/outputUntil",
    "repeat": "true"
}

PowerShell コマンド

注意

Azure を操作するには、Azure Az PowerShell モジュールを使用することをお勧めします。 作業を開始するには、Azure PowerShell のインストールに関する記事を参照してください。 Az PowerShell モジュールに移行する方法については、「AzureRM から Az への Azure PowerShell の移行」を参照してください。

これらのコマンドは、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"
        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"
    }

    Start-Sleep -Seconds 15
}

サポートされている他の制御フロー アクティビティを参照してください。