【发布时间】:2017-12-07 16:08:56
【问题描述】:
我创建了一个 CosmosDB 数据库,其中包含一个名为 MyTable 的表。我的想法是我想从 Azure 函数插入到这个表中。环顾四周,我想出了这个功能:
public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, out object tableOutput)
{
log.Info("C# HTTP trigger function processed a request.");
// parse query parameter
string field1 = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "field1", true) == 0)
.Value;
string field2 = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "field2", true) == 0)
.Value;
var item = new
{
field1 = field1,
field2 = field2
};
tableOutput = item;
return req.CreateResponse(HttpStatusCode.OK);
}
我得到的错误是这样的:
2017-12-07T15:52:44.066 执行函数时出现异常: 函数.MyFunc。 Microsoft.Azure.WebJobs.Host:处理时出错 函数返回后的参数tableOutput:。 Microsoft.Azure.WebJobs.Extensions.DocumentDB:集合 “tableOutput”(在数据库“myCosmosDB”中)不存在。到 自动创建集合,将“CreateIfNotExists”设置为 '真的'。 Microsoft.Azure.Documents.Client:消息:{“错误”:[“所有者 资源不存在"]}
我已经设置了输出参数,并且我看到了这里提到的复选框(CreateIfNotExists);但是,我有一个现有的 Cosmos 表设置,并想写信给它;所以我的问题是,如何从 Azure 函数中访问该表?
function.json 如下:
{
"bindings": [
{
"authLevel": "function",
"name": "req",
"type": "httpTrigger",
"direction": "in"
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"type": "documentDB",
"name": "outputDocument",
"databaseName": "mycosmosdb",
"collectionName": "tableOutput",
"createIfNotExists": false,
"connection": "mycostmosdb_DOCUMENTDB",
"direction": "out"
}
],
"disabled": false
}
编辑:
有两个人暗示,就这个问题而言,术语表和集合是同义词。如果我理解正确,那么情况似乎并非如此,因为即使我更改 function.json 中的集合名称以匹配我创建的表的名称,我也会收到相同的错误。
为了阐明 CosmosDB 表配置,我在数据资源管理器中看到一个节点授权 TablesDB,它具有我创建的表的子节点。
【问题讨论】:
-
请分享您的
function.json -
我已经用 function.json 更新了问题
-
我是 CosmosDB 的新手,但据我了解,表和集合是不同类型的东西。如果我是正确的,那么我想更新一个表,而不是一个集合(显然,如果我错了,那么这可能是问题的答案)。
-
@pm_2 在 Cosmos DB 中不存在您习惯使用的 SQL 数据库中的表。它们都是 Cosmos DB 中的文档集合,但如果您使用 Table API,那么它基本上只是将它们称为 Tables。
-
所以,需要明确的是,Azure 在其 UI 中指代表的地方,它意味着一个集合,这就是它正在寻找的东西
标签: c# azure azure-cosmosdb azure-functions