快速入門:使用 Azure CLI 建立 Azure 資料處理站

本快速入門說明如何使用 Azure CLI 來建立 Azure 資料處理站。 在這些資料處理站中建立的管道會將資料從 Azure Blob 儲存體中的一個資料夾複製到其他資料夾。 如需如何使用 Azure Data Factory 轉換資料的資訊,請參閱 Azure Data Factory 中的資料轉換

如需 Azure Data Factory 服務簡介,請參閱 Azure Data Factory 簡介

如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶

必要條件

注意

若要建立 Data Factory 執行個體,您用來登入 Azure 的使用者帳戶必須是 參與者 或 擁有者 角色的成員,或是 Azure 訂用帳戶的 系統管理員。 如需詳細資訊,請參閱 Azure 角色

準備容器和測試檔案

本快速入門使用 Azure 儲存體帳戶,其中包含具有檔案的容器。

  1. 若要建立名稱為 ADFQuickStartRG 資源群組,請使用 az group create 命令:

    az group create --name ADFQuickStartRG --location eastus
    
  2. 使用 az storage account create 命令,建立儲存體帳戶:

    az storage account create --resource-group ADFQuickStartRG \
        --name adfquickstartstorage --location eastus
    
  3. 使用 az storage container create 命令,建立名稱為 adftutorial 的容器:

    az storage container create --resource-group ADFQuickStartRG --name adftutorial \
        --account-name adfquickstartstorage --auth-mode key
    
  4. 在本機目錄中,建立名稱為 emp.txt 的檔案以上傳。 如果您在 Azure Cloud Shell 中工作,您可以使用 echo $PWD Bash 命令來尋找目前的工作目錄。 您可以使用標準 Bash 命令 (例如 cat) 建立檔案:

    cat > emp.txt
    This is text.
    

    使用 Ctrl+D 儲存新檔案。

  5. 若要將新檔案上傳至您的 Azure 儲存體容器,請使用 az storage blob upload 命令:

    az storage blob upload --account-name adfquickstartstorage --name input/emp.txt \
        --container-name adftutorial --file emp.txt --auth-mode key
    

    此命令會上傳至名稱為 input 的新資料夾。

建立資料處理站

若要建立 Azure Data Factory,請執行 az datafactory create 命令:

az datafactory create --resource-group ADFQuickStartRG \
    --factory-name ADFTutorialFactory

重要

以全域唯一的資料處理站名稱取代 ADFTutorialFactory,例如 ADFTutorialFactorySP1127。

您可以使用 az datafactory show 命令來查看您建立的資料處理站:

az datafactory show --resource-group ADFQuickStartRG \
    --factory-name ADFTutorialFactory

建立連結服務和資料集

接下來,建立連結服務和資料集。

  1. 使用 az storage account show-connection-string 命令,取得儲存體帳戶的連接字串:

    az storage account show-connection-string --resource-group ADFQuickStartRG \
        --name adfquickstartstorage --key primary
    
  2. 在您的工作目錄中,使用此內容建立 JSON 檔案,其中包含上一個步驟中的您自己的連接字串。 將檔案命名為 AzureStorageLinkedService.json

    {
        "type": "AzureBlobStorage",
        "typeProperties": {
            "connectionString": "DefaultEndpointsProtocol=https;AccountName=<accountName>;AccountKey=<accountKey>;EndpointSuffix=core.windows.net"
        }
    }
    
  3. 使用 az datafactory linked-service create 命令建立名稱為 AzureStorageLinkedService 的連結服務:

    az datafactory linked-service create --resource-group ADFQuickStartRG \
        --factory-name ADFTutorialFactory --linked-service-name AzureStorageLinkedService \
        --properties AzureStorageLinkedService.json
    
  4. 在您的工作目錄中,使用此內容建立 JSON 檔案,名稱為 InputDataset.json

    {
        "linkedServiceName": {
            "referenceName": "AzureStorageLinkedService",
            "type": "LinkedServiceReference"
        },
        "annotations": [],
        "type": "Binary",
        "typeProperties": {
            "location": {
                "type": "AzureBlobStorageLocation",
                "fileName": "emp.txt",
                "folderPath": "input",
                "container": "adftutorial"
            }
        }
    }
    
  5. 使用 az datafactory dataset create 名稱為 InputDataset 的輸入資料集:

    az datafactory dataset create --resource-group ADFQuickStartRG \
        --dataset-name InputDataset --factory-name ADFTutorialFactory \
        --properties InputDataset.json
    
  6. 在您的工作目錄中,使用此內容建立 JSON 檔案,名稱為 OutputDataset.json

    {
        "linkedServiceName": {
            "referenceName": "AzureStorageLinkedService",
            "type": "LinkedServiceReference"
        },
        "annotations": [],
        "type": "Binary",
        "typeProperties": {
            "location": {
                "type": "AzureBlobStorageLocation",
                "folderPath": "output",
                "container": "adftutorial"
            }
        }
    }
    
  7. 使用 az datafactory dataset create 名稱為 OutputDataset 的輸出資料集:

    az datafactory dataset create --resource-group ADFQuickStartRG \
        --dataset-name OutputDataset --factory-name ADFTutorialFactory \
        --properties OutputDataset.json
    

建立並執行管線

最後,建立並執行管道。

  1. 在您的工作目錄中,使用名稱為 Adfv2QuickStartPipeline.json 的這個內容建立 JSON 檔案:

    {
        "name": "Adfv2QuickStartPipeline",
        "properties": {
            "activities": [
                {
                    "name": "CopyFromBlobToBlob",
                    "type": "Copy",
                    "dependsOn": [],
                    "policy": {
                        "timeout": "7.00:00:00",
                        "retry": 0,
                        "retryIntervalInSeconds": 30,
                        "secureOutput": false,
                        "secureInput": false
                    },
                    "userProperties": [],
                    "typeProperties": {
                        "source": {
                            "type": "BinarySource",
                            "storeSettings": {
                                "type": "AzureBlobStorageReadSettings",
                                "recursive": true
                            }
                        },
                        "sink": {
                            "type": "BinarySink",
                            "storeSettings": {
                                "type": "AzureBlobStorageWriteSettings"
                            }
                        },
                        "enableStaging": false
                    },
                    "inputs": [
                        {
                            "referenceName": "InputDataset",
                            "type": "DatasetReference"
                        }
                    ],
                    "outputs": [
                        {
                            "referenceName": "OutputDataset",
                            "type": "DatasetReference"
                        }
                    ]
                }
            ],
            "annotations": []
        }
    }
    
  2. 使用 az datafactory pipeline create 命令建立名稱為 Adfv2QuickStartPipeline 的管道:

    az datafactory pipeline create --resource-group ADFQuickStartRG \
        --factory-name ADFTutorialFactory --name Adfv2QuickStartPipeline \
        --pipeline Adfv2QuickStartPipeline.json
    
  3. 使用 az datafactory pipeline create-run 命令執行管道 :

    az datafactory pipeline create-run --resource-group ADFQuickStartRG \
        --name Adfv2QuickStartPipeline --factory-name ADFTutorialFactory
    

    此命令會傳回執行識別碼。 複製這個識別碼,以便在下個命令中使用。

  4. 使用 az datafactory pipeline-run show 命令來確認管道執行成功:

    az datafactory pipeline-run show --resource-group ADFQuickStartRG \
        --factory-name ADFTutorialFactory --run-id 00000000-0000-0000-0000-000000000000
    

您也可以使用 Azure 入口網站,確認管道如預期執行。 如需詳細資訊,請參閱檢閱已部署的資源

清除資源

本快速入門中的所有資源都是相同資源群組的一部分。 若要將全部這些移除,請使用 az group delete 命令:

az group delete --name ADFQuickStartRG

如果您要將此資源群組用於其他任何專案,請改為刪除個別資源。 例如,若要移除連結服務,請使用 az datafactory linked-service delete 命令。

在本快速入門中,您已建立下列 JSON 檔案:

  • AzureStorageLinkedService.json
  • InputDataset.json
  • OutputDataset.json
  • Adfv2QuickStartPipeline.json

使用標準 Bash 命令加以刪除。