使用 PowerShell 建立資料處理站管線,以在雲端中複製資料
這個範例 PowerShell 指令碼會在 Azure Data Factory 中建立管線,以將資料從 Azure Blob 儲存體中的一個位置複製到另一個位置。
注意
建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 若要開始使用,請參閱 安裝 Azure PowerShell。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az。
此範例需要 Azure PowerShell。 執行 Get-Module -ListAvailable Az
以尋找版本。
如果您需要安裝或升級,請參閱安裝 Azure PowerShell 模組。
執行 Connect-AzAccount Cmdlet 以連線到 Azure。
必要條件
- Azure 儲存體帳戶。 您需要使用 Blob 儲存體作為來源和接收資料存放區。 如果您沒有 Azure 儲存體帳戶,請參閱建立儲存體帳戶來建立帳戶。
- 在 Blob 儲存體中建立一個 Blob 容器,在容器中建立一個輸入資料夾,然後上傳一些檔案到資料夾中。 您可以使用 Azure 儲存體總管之類的工具連線到 Azure Blob 儲存體,建立 Blob 容器,上傳輸入檔,以及驗證輸出檔。
範例指令碼
重要
這個指令碼會建立 JSON 檔案,該檔案定義硬碟上 c:\ 資料夾中的 Data Factory 實體 (已連結的服務、資料集和管線)。
# Set variables with your own values
$resourceGroupName = "<Azure resource group name>"
$dataFactoryName = "<Data factory name>" # must be globally unquie
$dataFactoryRegion = "East US"
$storageAccountName = "<Az.Storage account name>"
$storageAccountKey = "<Az.Storage account key>"
$sourceBlobPath = "<Azure blob container name>/<Azure blob input folder name>" # example: adftutorial/input
$sinkBlobPath = "<Azure blob container name>/<Azure blob output folder name>" # example: adftutorial/output
$pipelineName = "CopyPipeline"
# Create a resource group
New-AzResourceGroup -Name $resourceGroupName -Location $dataFactoryRegion
# Create a data factory
$df = Set-AzDataFactoryV2 -ResourceGroupName $resourceGroupName -Location $dataFactoryRegion -Name $dataFactoryName
# Create an Az.Storage linked service in the data factory
## JSON definition of the linked service.
$storageLinkedServiceDefinition = @"
{
"name": "AzureStorageLinkedService",
"properties": {
"type": "AzureStorage",
"typeProperties": {
"connectionString": {
"value": "DefaultEndpointsProtocol=https;AccountName=$storageAccountName;AccountKey=$storageAccountKey",
"type": "SecureString"
}
}
}
}
"@
## IMPORTANT: stores the JSON definition in a file that will be used by the Set-AzDataFactoryV2LinkedService command.
$storageLinkedServiceDefinition | Out-File ./StorageLinkedService.json
## Creates a linked service in the data factory
Set-AzDataFactoryV2LinkedService -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -Name "AzureStorageLinkedService" -File ./StorageLinkedService.json
# Create an Azure Blob dataset in the data factory
## JSON definition of the dataset
$datasetDefiniton = @"
{
"name": "BlobDataset",
"properties": {
"type": "AzureBlob",
"typeProperties": {
"folderPath": {
"value": "@{dataset().path}",
"type": "Expression"
}
},
"linkedServiceName": {
"referenceName": "AzureStorageLinkedService",
"type": "LinkedServiceReference"
},
"parameters": {
"path": {
"type": "String"
}
}
}
}
"@
## IMPORTANT: store the JSON definition in a file that will be used by the Set-AzDataFactoryV2Dataset command.
$datasetDefiniton | Out-File ./BlobDataset.json
## Create a dataset in the data factory
Set-AzDataFactoryV2Dataset -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -Name "BlobDataset" -File "./BlobDataset.json"
# Create a pipeline in the data factory
## JSON definition of the pipeline
$pipelineDefinition = @"
{
"name": "$pipelineName",
"properties": {
"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"
}
}
}
],
"parameters": {
"inputPath": {
"type": "String"
},
"outputPath": {
"type": "String"
}
}
}
}
"@
## IMPORTANT: store the JSON definition in a file that will be used by the Set-AzDataFactoryV2Pipeline command.
$pipelineDefinition | Out-File ./CopyPipeline.json
## Create a pipeline in the data factory
Set-AzDataFactoryV2Pipeline -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -Name $pipelineName -File "./CopyPipeline.json"
# Create a pipeline run
## JSON definition for pipeline parameters
$pipelineParameters = @"
{
"inputPath": "$sourceBlobPath",
"outputPath": "$sinkBlobPath"
}
"@
## IMPORTANT: store the JSON definition in a file that will be used by the Invoke-AzDataFactoryV2Pipeline command.
$pipelineParameters | Out-File ./PipelineParameters.json
# Create a pipeline run by using parameters
$runId = Invoke-AzDataFactoryV2Pipeline -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -PipelineName $pipelineName -ParameterFile ./PipelineParameters.json
# Check the pipeline run status until it finishes the copy operation
while ($True) {
$result = Get-AzDataFactoryV2ActivityRun -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName -PipelineRunId $runId -RunStartedAfter (Get-Date).AddMinutes(-30) -RunStartedBefore (Get-Date).AddMinutes(30)
if (($result | Where-Object { $_.Status -eq "InProgress" } | Measure-Object).count -ne 0) {
Write-Host "Pipeline run status: In Progress" -foregroundcolor "Yellow"
Start-Sleep -Seconds 30
}
else {
Write-Host "Pipeline '$pipelineName' run finished. Result:" -foregroundcolor "Yellow"
$result
break
}
}
# Get the activity run details
$result = Get-AzDataFactoryV2ActivityRun -DataFactoryName $dataFactoryName -ResourceGroupName $resourceGroupName `
-PipelineRunId $runId `
-RunStartedAfter (Get-Date).AddMinutes(-10) `
-RunStartedBefore (Get-Date).AddMinutes(10) `
-ErrorAction Stop
$result
if ($result.Status -eq "Succeeded") {`
$result.Output -join "`r`n"`
}`
else {`
$result.Error -join "`r`n"`
}
# To remove the data factory from the resource gorup
# Remove-AzDataFactoryV2 -Name $dataFactoryName -ResourceGroupName $resourceGroupName
#
# To remove the whole resource group
# Remove-AzResourceGroup -Name $resourceGroupName
清除部署
執行範例指令碼之後,您可以使用下列命令以移除資源群組及與其相關聯的所有資源:
Remove-AzResourceGroup -ResourceGroupName $resourceGroupName
若要從資源群組移除資料處理站,請執行下列命令:
Remove-AzDataFactoryV2 -Name $dataFactoryName -ResourceGroupName $resourceGroupName
指令碼說明
此指令碼會使用下列命令:
Command | 注意 |
---|---|
New-AzResourceGroup | 建立用來存放所有資源的資源群組。 |
Set-AzDataFactoryV2 | 建立資料處理站。 |
Set-AzDataFactoryV2LinkedService | 在資料處理站中建立已連結的服務。 已連結的服務會將資料存放區或計算連結到資料處理站。 |
Set-AzDataFactoryV2Dataset | 在資料處理站中建立資料集。 資料集代表管線中活動的輸入/輸出。 |
Set-AzDataFactoryV2Pipeline | 在資料處理站中建立管線。 管線包含一個或多個執行特定作業的活動。 在此管線中,複製活動會將資料從 Azure Blob 儲存體中的一個位置複製到其他位置。 |
Invoke-AzDataFactoryV2Pipeline | 建立管線的執行。 也就是說,執行管線。 |
Get-AzDataFactoryV2ActivityRun | 在管線中取得活動的執行 (活動執行) 的相關詳細資料。 |
Remove-AzResourceGroup | 刪除資源群組,包括所有的巢狀資源。 |
相關內容
如需有關 Azure PowerShell 的詳細資訊,請參閱 Azure PowerShell 文件。
您可以在 Azure Data Factory PowerShell 範例中,找到其他 Azure Data Factory PowerShell 指令碼範例。