How to store json content to CosmosDB with CreateDocumentAsync

亡梦爱人 提交于 2019-12-14 02:41:32

问题


TARGET: Create Console Application, which 1) Read json from Azure Data Lake Store 2) Store data to Cosmos DB as json. (In next step I will parse json contents in code before storing)

PROBLEM: New Document is created in CosmosDB, but it contains some kind of meta data instead of actual json file content. String json do include content I need, but it is not passed correctly to CreateDocumentAsync. I would appreciate help with fixing issue and also appreciate tips to make code easy to handle json.

ERROR: No error messages, but wrong content is stored to CosmosDB.

CODE:

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(I want to store this):

[
{
    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 STORED TO 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
}

回答1:


As you mentioned that you store the different json to the documentDb, the root reason is that you store the JsonTextReader object to documentDB.

If you want to store the Jarry to the documentDB, you could store it as a property value of a Json object.

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

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

Note: Based on my test it is not supported to store Jarray object directly to the documentDb. I test it with Azure portal.



来源:https://stackoverflow.com/questions/48358841/how-to-store-json-content-to-cosmosdb-with-createdocumentasync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!