簡體   English   中英

如何使用 CreateDocumentAsync 將 json 內容存儲到 CosmosDB

[英]How to store json content to CosmosDB with CreateDocumentAsync

目標:創建控制台應用程序,其中 1) 從 Azure Data Lake Store 讀取 json 2) 將數據存儲到 Cosmos DB 中作為 json。(在下一步中,我將在存儲之前解析代碼中的 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存儲到documentDb一樣,根本原因是將JsonTextReader對象存儲到documentDB。

如果要將Jarry存儲到documentDB,則可以將其作為Json對象的屬性值存儲。

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

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

在此處輸入圖片說明

注意:根據我的測試,不支持將Jarray對象直接存儲到documentDb。 我使用Azure門戶對其進行了測試。

我遇到了同樣的問題。 您需要將 JsonProperty 屬性放在您的屬性上。 像這樣的東西

    [JsonProperty(PropertyName = "LastUpdatedByName")]
    public string LastUpdatedByName { get; set; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM