Azure函数C#:根据HTTP请求在cosmos db中创建或替换文档 [英] Azure function C#: Create or replace document in cosmos db on HTTP request
问题描述
我正在尝试在C#中构建Azure函数,如果还没有ID,则使用SQL API在Azure cosmos DB中创建一个新的文档对象,并更新一个已经存在的文档对象.
I'm trying to build an Azure function in C# that creates a new document object in Azure cosmos DB using SQL API if an id doesn't already exist and updates a document object if it already exists.
其背后的背景是将聊天机器人的对话历史记录到唯一的用户会话中.
The context behind this is logging chatbot conversation history to unique user sessions.
输入:
带有参数(id(字符串),chatHistory(字符串)和chatDateTime(字符串))的HTTP GET请求
Input:
HTTP GET Request with parameters (id (string), chatHistory(string) and chatDateTime(string))
输出:
如果具有相同ID的文档对象已经存在-请使用输入chatHisotry和chatDateTime更新文档.
如果不存在具有相同ID的文档对象,则创建一个ID,chatHistory和chatDateTime等于输入的新文档对象.
Output:
If document object with same id already exists - then update document with input chatHisotry and chatDateTime.
If no document object exists with same id then create a new document object with id, chatHistory and chatDateTime equal to input.
任何帮助,不胜感激!挣扎了好几天.
Any help much appreciated! Been struggling with this one for days.
文档对象示例:
{
"id": "ESCRfAKwlTbH8W5aVRLxgA",
"chatHistory": "Hi, Hello",
"chatDateTime": "Fri Sep 21 2018 05:34:35 GMT+0000 (Coordinated Universal Time)",
"_rid": "RwYSAIqaSVg2AAAAAAAAAA==",
"_self": "dbs/RwYSAA==/colls/RwYSAIqaSVg=/docs/RwYSAIqaSVg2AAAAAAAAAA==/",
"_etag": "\"00007400-0000-0000-0000-5ba482ed0000\"",
"_attachments": "attachments/",
"_ts": 1537508077
}
推荐答案
You can use the Azure Functions' Cosmos DB Output Binding. The Output binding does an Upsert operation.
[FunctionName("HttpTriggerWithSingleDocument")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req,
[DocumentDB(databaseName: "your-db",
collectionName: "your-collection",
ConnectionStringSetting = "CosmosDBConnectionString")] out dynamic documentToSave)
{
dynamic data = await req.Content.ReadAsAsync<object>();
if (data == null)
{
documentToSave = null;
return req.CreateResponse(HttpStatusCode.BadRequest);
}
documentToSave = data;
return req.CreateResponse(HttpStatusCode.Created);
}
Azure Portal版本:
Azure Portal version:
using System.Net;
public static async Task<HttpResponseMessage> Run(
HttpRequestMessage req,
IAsyncCollector<dynamic> documentsToStore)
{
dynamic data = await req.Content.ReadAsAsync<object>();
if (data == null)
{
return req.CreateResponse(HttpStatusCode.BadRequest);
}
await documentsToStore.AddAsync(data);
return req.CreateResponse(HttpStatusCode.Created);
}
您还需要将function.json
更新为以下内容:
And you also need the function.json
updated to something like:
{
"bindings": [
{
"name": "req",
"type": "httpTrigger",
"direction": "in"
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"type": "documentDB",
"name": "documentsToStore",
"databaseName": "<your-database-name>",
"collectionName": "<your-collection-name>",
"createIfNotExists": false,
"connection": "<your-connection-setting-name>",
"direction": "out"
}
]
}
可在此处获得更多示例: https://github.com/ealsur/serverless-recipes/tree/master/cosmosdboutputbindings
More samples available here: https://github.com/ealsur/serverless-recipes/tree/master/cosmosdboutputbindings
这篇关于Azure函数C#:根据HTTP请求在cosmos db中创建或替换文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!