简体   繁体   English

将原始 JSON 写入 CosmosDB 和一个 Azure Function

[英]Writing raw JSON to CosmosDB and an Azure Function

I want to take the raw JSON body from an HTTP post and write it directly into my CosmosDB.我想从 HTTP 帖子中获取原始 JSON 正文并将其直接写入我的 CosmosDB。

Let's say the data looks like this:假设数据如下所示:

{
  "id": "123456",
  "storeName": "City Bar and Grille",
  "invoiceTotal": 65
}

However, the documentsOut.AddAsync command uses a format like this:但是,documentsOut.AddAsync 命令使用如下格式:

wait documentsOut.AddAsync(new
  {
    // create a random ID
    id = System.Guid.NewGuid().ToString(),
    body = sourceJson
  });

And then I end up with a document that looks like this:然后我得到一个如下所示的文档:

{
  "id": "0e99d3ab-1956-4c0a-8ec1-99de5c987555",
  "body": {
    "id": "123456",
    "storeName": "City Bar and Grille",
    "invoiceTotal": 65
  }
}

What I really want is to end up with this:我真正想要的是这样结束:

{
  "id": "123456",
  "storeName": "City Bar and Grille",
  "invoiceTotal": 65
}

I'd like to drop the id = System.Guid.NewGuid().ToString() completely (which should not be hard).我想完全删除 id = System.Guid.NewGuid().ToString() (这应该不难)。

How can I pass the raw JSON through without needing to add it to some parent node (such as body)?如何传递原始 JSON 而无需将其添加到某些父节点(例如正文)?

Using a similar example as shared by you in the question.使用您在问题中分享的类似示例。 We can create a Model class with properties which want to store in database.我们可以创建一个 Model class 具有要存储在数据库中的属性。

在此处输入图像描述

public class StoreDetails : TableEntity
{
    [JsonProperty("id")]
    public string Id { get; set; } 
    [JsonProperty("storeName")]
    public string StoreName { get; set; }
    [JsonProperty("invoiceTotal")]
    public string InvoiceTotal { get; set; }
}

Then can create object of model and pass that object to AddAsync() method.然后可以创建 model 的 object 并将该 object 传递给AddAsync()方法。 As shown in below screenshot.如下截图所示。

在此处输入图像描述

        var item = new StoreDetails
        {
            Id = Guid.NewGuid().ToString(),
            StoreName = data.StoreName,
            InvoiceTotal = data.InvoiceTotal
        };
        await storeDetailOut.AddAsync(item);

and when we triggered post API, got response as shown below.当我们触发 post API 时,得到如下所示的响应。

在此处输入图像描述

As finally we can see the same record is stored in CosmosDB.最后我们可以看到相同的记录存储在 CosmosDB 中。

在此处输入图像描述

Just to formalize my comment as an answer: You're specifically creating the body property of the Cosmos DB document you're creating:只是为了将我的评论形式化为一个答案:你专门创建了你正在创建的 Cosmos DB 文档的body属性:

wait documentsOut.AddAsync(new
  {
    // create a random ID
    id = System.Guid.NewGuid().ToString(),
    body = sourceJson
  });

At the same time, you're ignoring the incoming ID.同时,您忽略了传入的 ID。 Since you wanted to preserve that ID, You can copy the ID over (as long as it remains unique within the partition) instead of generating a new GUID, and also grab individual properties from sourceJson to avoid the nested body element:由于您想保留该 ID,您可以复制该 ID(只要它在分区中保持唯一)而不是生成新的 GUID,并且还可以从sourceJson获取各个属性以避免嵌套的body元素:

wait documentsOut.AddAsync(new
  {
    id = sourceJson.id,
    storeName = sourceJson.storeName,
    invoiceTotal = sourceJson.invoiceTotal
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CosmosDB Azure function 触发器与 SignalR output 绑定 - CosmosDB Azure function Trigger with SignalR output binding 使用来自 Azure 的托管身份连接 Azure CosmosDB 在本地和 Azure 上的应用程序 Function - Connect Azure CosmosDB using Managed Identities from Azure Function App both locally and on Azure Azure cosmosdb 模拟器问题 - Azure cosmosdb emulator issue 如何将 CosmosDB 依赖项跟踪数据从 Azure Function 事件中心触发器发送到 Application Insights - How to send CosmosDB dependency tracking data to Application Insights from Azure Function Event Hub Trigger Azure CosmosDB 用户定义 Function 返回等于表中记录总数的记录 - Azure CosmosDB User Defined Function returns records equal to the total number of records in the table Azure CosmosDB - 地理参考数据的分片策略? - Azure CosmosDB - Sharding Strategy for georeferenced data? 如何使用 CreateDocumentAsync 将 json 内容存储到 CosmosDB - How to store json content to CosmosDB with CreateDocumentAsync Azure 数据工厂复制到 CosmosDB 节流 - Azure Data Factory copy to CosmosDB throttling Azure function C#:写入 Azure 上的块 blob(csv 文件)存储帐户创建了两个版本的 blob - Azure function C#: Writing to Block blob (csv file) on Azure Storage Account creates two versions of the blob 如何 SQL 在 MS Azure CosmosDB 中查询嵌套列表和数组? - How to SQL query a nested list and array in a MS Azure CosmosDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM