有没有一种方法可以在Azure数据工厂中插入带有嵌套数组的文档?

时间:2018-07-19 19:47:32

标签: azure-cosmosdb azure-data-factory

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

样本文档:

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

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

是否可以配置复制活动以处理此阵列?

2 个答案:

答案 0 :(得分:2)

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

由于您正在使用adf导入数据,因此无法使用PreTrigger更改已创建文档的格式。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);
                    }

                }
            }
        }
    }
}

希望它对您有帮助。

答案 1 :(得分:2)

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