首页 > 解决方案 > 如何使用提交到 git 存储库的 Azure 数据工厂 json 文件通过代码创建组件

问题描述

我不知道这是否应该是这样使用的,但我是这样看待这个过程的:

  1. 在连接到我的 git 存储库的 Azure 数据工厂工作室中创建/编辑组件。
  2. 提交对 repo 的更改以创建 json 定义文件
  3. 在 C# 程序中使用这些文件在实时环境中“创建或更新”我的组件

以下是我提交更改时生成的文件的一些示例:

Test3Json.json:

    {
        "name": "Test3Json",
        "properties": {
            "linkedServiceName": {
                "referenceName": "AzureBlobStorage1",
                "type": "LinkedServiceReference"
            },
            "parameters": {
                "FullPath": {
                    "type": "string"
                }
            },
            "annotations": [],
            "type": "Json",
            "typeProperties": {
                "location": {
                    "type": "AzureBlobStorageLocation",
                    "fileName": {
                        "value": "@dataset().FullPath",
                        "type": "Expression"
                    }
                }
            },
            "schema": {}
        }
    }

Test2JsonOutput.json:

    {
        "name": "Test2JsonOutput",
        "properties": {
            "linkedServiceName": {
                "referenceName": "AzureBlobStorage1",
                "type": "LinkedServiceReference"
            },
            "parameters": {
                "OutputPath": {
                    "type": "string"
                }
            },
            "annotations": [],
            "type": "Json",
            "typeProperties": {
                "location": {
                    "type": "AzureBlobStorageLocation",
                    "folderPath": {
                        "value": "@dataset().OutputPath",
                        "type": "Expression"
                    },
                    "container": "reporting"
                }
            },
            "schema": {}
        },
        "type": "Microsoft.DataFactory/factories/datasets"
    }

Test3Dataflow.json:

    {
        "name": "Test3Dataflow",
        "properties": {
            "type": "MappingDataFlow",
            "typeProperties": {
                "sources": [
                    {
                        "dataset": {
                            "referenceName": "Test3Json",
                            "type": "DatasetReference"
                        },
                        "name": "source1"
                    }
                ],
                "sinks": [
                    {
                        "dataset": {
                            "referenceName": "Test2JsonOutput",
                            "type": "DatasetReference"
                        },
                        "name": "sink1"
                    }
                ],
                "transformations": [],
                "script": "source(allowSchemaDrift: true,\n\tvalidateSchema: false,\n\tignoreNoFilesFound: false,\n\tdocumentForm: 'documentPerLine') ~> source1\nsource1 sink(allowSchemaDrift: true,\n\tvalidateSchema: false,\n\tskipDuplicateMapInputs: true,\n\tskipDuplicateMapOutputs: true) ~> sink1"
            }
        }
    }

然后我根据这篇文章,说明可以通过代码创建或更新组件。

为了在 C# 程序中创建数据集并基于 json 文件,我使用了类似的东西。它正在创建它,但它似乎无法使用,因为在创建后在 Data Factory Studio 中查看它时缺少属性:

public class AzureDataFactoryResourceWrapper<T>
{
    public string Name { get; set; }
    public T Properties { get; set; }
    public string Type { get; set; }
}

    private async Task CreateDataSet(string jsonDefinition)
    {
        var resource = JsonSerializer.Deserialize<AzureDataFactoryResourceWrapper<AzureBlobDataset>>(jsonDefinition, optionsCaseInsensitive);
        var dataset = new DatasetResource(resource.Properties);

        await client.Datasets.CreateOrUpdateAsync(resourceGroup, dataFactoryName, resource.Name, dataset);
    }

但是,当我尝试对 DataFlow 执行相同操作时,似乎 json 文件中定义的属性与DataFlowResourceC# 类的定义不匹配,在将 json 反序列化为对象后,我到处都得到 NULL。

    private async Task CreateDataFlow(string jsonDefinition)
    {
        var resource = JsonSerializer.Deserialize<AzureDataFactoryResourceWrapper<DataFlowResource>>(jsonDefinition, optionsCaseInsensitive);
        var dataFlow = resource.Properties;

        await client.DataFlows.CreateOrUpdateAsync(resourceGroup, dataFactoryName, resource.Name, dataFlow);
    }

json 文件是否应该可用于通过代码创建组件?

这是正确的方法吗?

如果无法使用 json 文件来创建组件,那么将 git 与数据工厂一起使用的目的是什么?

标签: c#azure.net-coreazure-data-factory

解决方案


推荐阅读