共用方式為


快速入門:使用 Azure CLI 建立 Azure Data Factory

本快速入門說明如何使用 Azure CLI 來建立 Azure Data Factory。 您在此數據處理站中建立的管線會將資料從一個資料夾複製到 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. 使用 adftutorial 命令建立名為的容器:

    az storage container create --resource-group ADFQuickStartRG --name adftutorial \
        --account-name adfquickstartstorage --auth-mode key
    
  4. 在本機目錄中,建立名為 to upload 的 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 資料處理站,請執行 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. 使用 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. 使用 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. 使用 OutputDataset 命令建立名為的輸出資料集:

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

建立並執行管線

最後,建立並執行流水線。

  1. 在您的工作目錄中,建立具有以下內容的 Adfv2QuickStartPipeline.jsonJSON 檔案:

    {
        "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. 使用 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 命令刪除它們。