有没有办法在 Azure 数据工厂中插入带有嵌套数组的文档?

Is there a way to insert a document with a nested array in Azure Data Factory?

我正在尝试在具有嵌套数组的 CosmosDb 中添加文档。我正在使用 Copy Activity

示例文档:

{
  "itemNumber": "D10001",      
  "readings" : [
                  { "value": 25, "ets":"100011111"},
                  { "value": 35, "ets":"100011122"}
               ]
}

在源数据集中,我在 SQL 查询中将读数数组格式化为 string,并将接收器数据集中的数据类型设置为 Object。数据被复制,但读数被字符串化。

有没有办法配置 Copy Activity 来处理这个数组?

据我所知,没有这样的属性可以帮助您在 adf cosmos db 配置中将字符串数据转换为数组格式。

由于您使用adf导入数据,所以您无法使用PreTrigger更改创建的格式documents.PreTrigger需要通过代码调用或rest api。

因此,作为解决方法,我建议您使用 Azure Function Cosmos DB Trigger 来处理每个导入数据库的文档。请参考我的功能代码:

using System.Collections.Generic;
using Microsoft.Azure.Documents;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json.Linq;
using System;
using Microsoft.Azure.Documents.Client;

namespace TestADF
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([CosmosDBTrigger(
            databaseName: "db",
            collectionName: "item",
            ConnectionStringSetting = "documentdbstring",
            LeaseCollectionName = "leases")]IReadOnlyList<Document> input, TraceWriter log)
        {
            if (input != null && input.Count > 0)
            {
                log.Verbose("Start.........");
                String endpointUrl = "https://***.documents.azure.com:443/";
                String authorizationKey = "key";
                String databaseId = "db";
                String collectionId = "item";

                DocumentClient client = new DocumentClient(new Uri(endpointUrl), authorizationKey);

                for (int i = 0; i < input.Count; i++)
                {
                    Document doc = input[i];
                    if ((doc.GetPropertyValue<Boolean>("alreadyForamt") == null) || (!doc.GetPropertyValue<Boolean>("alreadyForamt")))
                    {                       
                        String readings = doc.GetPropertyValue<String>("readings");
                        JArray r = JArray.Parse(readings);

                        doc.SetPropertyValue("readings", r);

                        client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, doc.Id), doc);

                        log.Verbose("Update document Id " + doc.Id);
                    }

                }
            }
        }
    }
}

希望对您有所帮助。

你的来源是什么?您可以先将数据复制到 json 个文件。然后按原样将其导入 cosmos DB,这意味着不要在源和汇数据集中指定格式和结构。