【问题标题】:How to store json content to CosmosDB with CreateDocumentAsync如何使用 CreateDocumentAsync 将 json 内容存储到 CosmosDB
【发布时间】:2018-06-29 17:48:17
【问题描述】:

目标:创建控制台应用程序,它 1) 从 Azure Data Lake Store 读取 json 2) 将数据作为 json 存储到 Cosmos DB。 (在下一步中,我将在存储之前解析代码中的 json 内容)

问题:在 CosmosDB 中创建了新文档,但它包含某种元数据,而不是实际的 json 文件内容。字符串 json 确实包含我需要的内容,但它没有正确传递给 CreateDocumentAsync。我会很感激解决问题的帮助,也很感激让代码易于处理 json 的提示。

错误:没有错误消息,但错误的内容存储到 CosmosDB。

代码:

private  async Task CreateDocumentsAsync()
    {
        string fileName = "/myjson.json";

        // Obtain AAD token for ADLS 
        var creds = new ClientCredential(applicationId, clientSecret);
        var clientCreds = ApplicationTokenProvider.LoginSilentAsync(tenantId, creds).GetAwaiter().GetResult();

        // Create ADLS client object
        AdlsClient client = AdlsClient.CreateClient(adlsAccountFQDN, clientCreds);

        String json = "";

        //Read file contents
        using (var readStream = new StreamReader(client.GetReadStream(fileName)))
        {
            string line;
            while ((line = readStream.ReadLine()) != null)
            {
                Console.WriteLine("Read file Line: " + line);
                json += line;
            }
        }

        //Read file to json
        JsonTextReader reader = new JsonTextReader(new StringReader(json));

        //Storing json to CosmosDB
        Uri collectionUri = UriFactory.CreateDocumentCollectionUri(databaseName, collectionName);

        using (DocumentClient DocumentDBclient2 = new DocumentClient(new Uri(endpointUrl), authorizationKey))
        {
            Document doc = await DocumentDBclient2.CreateDocumentAsync(collectionUri, reader);

        }
    }

}

JSON SAMPLE(我想存储这个):

[
{
    color: "red",
    value: "#f00"
},
{
    color: "green",
    value: "#0f0"
},
{
    color: "blue",
    value: "#00f"
},
{
    color: "cyan",
    value: "#0ff"
},
{
    color: "magenta",
    value: "#f0f"
},
{
    color: "yellow",
    value: "#ff0"
},
{
    color: "black",
    value: "#000"
}
]

JSON 存储到 COSMOSDB:

{
"ArrayPool": null,
"LineNumber": 0,
"LinePosition": 0,
"CloseInput": true,
"SupportMultipleContent": false,
"QuoteChar": "\u0000",
"DateTimeZoneHandling": 3,
"DateParseHandling": 1,
"FloatParseHandling": 0,
"DateFormatString": null,
"MaxDepth": null,
"TokenType": 0,
"Value": null,
"ValueType": null,
"Depth": 0,
"Path": "",
"Culture": "(Default)",
"id": "0189f287-b6ae-4e45-95b8-879293d4c8f3",
"_rid": "nDxrAL1JbwYIAAAAAAAAAA==",
"_self": "dbs/nDxrAA==/colls/nDxrAL1JbwY=/docs/nDxrAL1JbwYIAAAAAAAAAA==/",
"_etag": "\"00006cc5-0000-0000-0000-5a633c900000\"",
"_attachments": "attachments/",
"_ts": 1516453008
}

【问题讨论】:

  • 请编辑您的问题并包含两件事:您发送的 JSON 数据和存储的实际值。
  • 感谢您有兴趣帮助我。我添加了示例 json。
  • @Keke - 没有提供足够的信息。例如:您谈到您的 JSON 内容存储不正确。很棒:显示您所看到的正在存储的内容。我们无法猜测您所说的“某种元数据”是什么意思。此外,您共享的示例 JSON:这正是 reader 中的内容吗?请编辑更清楚。如果没有更清楚的说明,这里发布的任何内容都只是猜测。
  • 谢谢。我添加了实际存储的 json。

标签: c# json azure azure-cosmosdb


【解决方案1】:

正如您提到的将不同的 json 存储到 documentDb,根本原因是您将 JsonTextReader 对象存储到 documentDB。

如果要将 Jarry 存储到 documentDB,可以将其存储为 Json 对象的属性值。

var jObject = new JObject {{"Data", json}};

Document doc = await DocumentDBclient2.CreateDocumentAsync(collectionUri, jObject);

注意:根据我的测试,不支持将 Jarray 对象直接存储到 documentDb。我使用 Azure 门户对其进行测试。

【讨论】:

    猜你喜欢
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多