c#使用Data Factory使用Where子句过滤器将SQL表数据复制到另一个数据库

时间:2018-08-07 09:46:22

标签: azure-data-factory-2

我正在使用c#代码通过数据工厂从一个SQL数据库(源)复制数据并移动到另一个SQL数据库(目标)的过程。 我能够将所有数据从源表复制到目标表,但是我只想移动过滤后的数据,例如SELECT * FROM Source.tbl WHERE Category =5。大约有10-15个表可以移动数据。您能提供给我帮助的示例代码吗?

我的用于移动单个表中所有数据的代码。

//验证并创建数据工厂管理客户端             var context = new AuthenticationContext(“ https://login.windows.net/” + tenantID);             ClientCredential cc =新的ClientCredential(AppID,AuthKey);             AuthenticationResult结果= context.AcquireTokenAsync(“ https://management.azure.com/”,cc).Result;             ServiceClientCredentials cred = new TokenCredentials(result.AccessToken);             var client = new DataFactoryManagementClient(cred){SubscriptionId = SubscriptionID};

        // Create data factory
        Factory dataFactory = new Factory { Location = Region, Identity = new FactoryIdentity() };

        // This line throws error, we cannot proceed further. unless we get access of creating DF or update or access.
        client.Factories.CreateOrUpdate(ResourceGroup, DataFactoryName, dataFactory);

        var DF = client.Factories.Get(ResourceGroup, DataFactoryName);

        while (DF.ProvisioningState == "PendingCreation")
        {
            System.Threading.Thread.Sleep(1000);
        }

        LinkedServiceResource storageLinkedService = new LinkedServiceResource(
            new AzureSqlDatabaseLinkedService
            {
                ConnectionString = new SecureString(SourceSQLConnString)
            }
        );
        client.LinkedServices.CreateOrUpdate(ResourceGroup, DataFactoryName, SourceSQLLinkedServiceName, storageLinkedService);

        LinkedServiceResource sqlDbLinkedService = new LinkedServiceResource(
            new AzureSqlDatabaseLinkedService
            {
                ConnectionString = new SecureString(DestSQLConnString)
            }
        );
        client.LinkedServices.CreateOrUpdate(ResourceGroup, DataFactoryName, DestSQLLinkedServiceName, sqlDbLinkedService);

        DatasetResource SourceSQLDataSet = new DatasetResource(
            new AzureSqlTableDataset
            {
                LinkedServiceName = new LinkedServiceReference
                {
                    ReferenceName = SourceSQLLinkedServiceName
                },
                TableName = Table, 
            }
        );
        client.Datasets.CreateOrUpdate(ResourceGroup, DataFactoryName, SourceSQLDataSetName, SourceSQLDataSet);

        // Create a Azure SQL Database dataset
        DatasetResource DestSQLDataSet = new DatasetResource(
            new AzureSqlTableDataset
            {
                LinkedServiceName = new LinkedServiceReference
                {
                    ReferenceName = DestSQLLinkedServiceName
                },
                TableName = Table
            }
        );
        client.Datasets.CreateOrUpdate(ResourceGroup, DataFactoryName, DestSQLDataSetName, DestSQLDataSet);

        PipelineResource pipeline = new PipelineResource
        {
            Activities = new List<Activity>
            {
                new CopyActivity
                {
                    Name = "CopyFromSQLToSQL",
                    Inputs = new List<DatasetReference>
                    {
                        new DatasetReference()
                        {
                            ReferenceName = SourceSQLDataSetName
                        }
                    },
                    Outputs = new List<DatasetReference>
                    {
                        new DatasetReference
                        {
                            ReferenceName = DestSQLDataSetName
                        }
                    },
                    Source = new SqlSource(),
                    Sink = new SqlSink { }
                }
            }
        };
        client.Pipelines.CreateOrUpdate(ResourceGroup, DataFactoryName, PipelineName, pipeline);

        // Create a pipeline run
        CreateRunResponse runResponse = client.Pipelines.CreateRunWithHttpMessagesAsync(ResourceGroup, DataFactoryName, PipelineName).Result.Body;

        // Monitor the pipeline run
        PipelineRun pipelineRun;
        while (true)
        {
            pipelineRun = client.PipelineRuns.Get(ResourceGroup, DataFactoryName, runResponse.RunId);
            if (pipelineRun.Status == "InProgress")
                System.Threading.Thread.Sleep(15000);
            else
                break;
        }

2 个答案:

答案 0 :(得分:0)

您可以将查询放入sql Source的SqlReaderQuery属性。

答案 1 :(得分:0)

我与Data Factory支持进行了交谈,他们说我们尚未实施, 创建数据工厂, 创建链接服务 循环创建数据集并创建复制活动