我正在使用 .NET SDK 以编程方式创建 Azure 数据工厂管道。我正在尝试使用 ForEachActivity 并在其中引用 DatasetReference 中的
@item()
。然而,似乎 @item()
正在计算为空字符串。下面是演示创建 ForEachActivity
和后续管道的代码片段:
ForEachActivity forEachActivity = new ForEachActivity
{
Name = "ForEachDbContainer",
BatchCount = 20, // You can adjust this based on your needs
//IsSequential = false,
Items = new Expression { Value = "@array(['Source_db1_container1'])" },
Activities = new List<Activity>
{
new CopyActivity
{
Name = "CosmosDBCopyActivity",
Inputs = new List<DatasetReference> { new DatasetReference { ReferenceName = "@item()" } },
Outputs = new List<DatasetReference> { new DatasetReference { ReferenceName = "Destination_db1_container1" } },
Source = new DocumentDbCollectionSource { },
Sink = new DocumentDbCollectionSink { },
}
}
};
PipelineResource pipeline = new PipelineResource
{
Activities = new List<Activity> { forEachActivity }
};
client.Pipelines.CreateOrUpdate(resourceGroupName, dataFactoryName, "YourPipelineName", pipeline);
执行上述代码时,我遇到以下错误:
Unhandled exception. Microsoft.Rest.Azure.CloudException: The document creation or update failed because of invalid reference ''.
任何人都可以帮我弄清楚为什么 @item() 计算结果为空字符串,以及如何解决这个问题?
您遇到的问题可能与您在
@item()
对象中使用 DatasetReference
表达式的方式有关。
@item()
表达式用于引用Items
的ForEachActivity
数组中的当前项。但是,在您的代码中,您使用 @item()
表达式作为 ReferenceName
对象的 DatasetReference
,这是不正确的。
您应该使用以下代码为每个活动创建,例如:
ForEachActivity forEachActivity = new ForEachActivity
{
Name = "ForEachDbContainer",
BatchCount = 20,
//IsSequential = false,
Items = new Expression { Value = "@CreateArray(["your-collection-1","Collection-2"])" },
Activities = new List<Activity>
{
new CopyActivity
{
Name = "CosmosDBCopyActivity",
Inputs = new List<DatasetReference>
{
new DatasetReference()
{
ReferenceName = datasetName //<your-source-datasetname>
}
},
Outputs = new List<DatasetReference>
{
new DatasetReference
{
ReferenceName = dataset2 //<Your-destination-datasetname>
}
},
Source = new DocumentDbCollectionSource { },
Sink = new DocumentDbCollectionSink { },
}
}
};
PipelineResource pipeline = new PipelineResource
{
Activities = new List<Activity> { forEachActivity }
};
client.Pipelines.CreateOrUpdate(resourceGroupName, dataFactoryName, "YourPipelineName", pipeline);
您可以使用以下代码为 cosmosdb 创建数据集。
为 cosmos DB 创建数据集时,在集合中将值指定为
"@item()"
代码:
var containerDataset = new DatasetResource
{
Properties = new CosmosDbSqlApiCollectionDataset
{
LinkedServiceName = new LinkedServiceReference { ReferenceName = "xxxxx" },
CollectionName = "@item()"
}
};
adfClient.Datasets.CreateOrUpdate(resourceGroupName, dataFactoryName, datasetName, containerDataset);
参考: