你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Azure CLI 创建 Azure 数据工厂

本快速入门介绍如何使用 Azure CLI 创建 Azure 数据工厂。 在此数据工厂中创建的管道将数据从一个文件夹复制到 Azure Blob 存储中的另一个文件夹。 有关如何使用 Azure 数据工厂转换数据的信息,请参阅 Azure 数据工厂中的数据转换

有关 Azure 数据工厂服务的简介,请参阅 Azure 数据工厂简介

如果没有 Azure 订阅,请在开始之前创建一个免费帐户

先决条件

注释

若要创建数据工厂实例,用于登录到 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. 在本地目录中,创建一个名为 emp.txt 上传的文件。 如果在 Azure Cloud Shell 中工作,可以使用 Bash 命令找到当前工作目录 echo $PWD 。 可以使用标准 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. 使用 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. 使用 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
    

    此命令返回运行 ID。 复制它以便在下一个命令中使用。

  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 命令删除它们。