【问题标题】:Adding to an Azure CosmosDB from inside an Azure Function从 Azure 函数内部添加到 Azure CosmosDB
【发布时间】: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


【解决方案1】:

您需要为您尝试将新文档保存到的 Cosmos DB 集合,使用正确的“collectionName”值更新 Function.json。目前,您的 Cosmos DB 数据库(“mycosmosdb”)中没有名为“tableOutput”的集合。

此外,如果您希望 Azure 函数输出绑定在集合不存在时自动创建集合,则将 Function.json 属性“createIfNotExists”设置为“true”,如果集合不存在,它将创建集合它不存在。

这两种方法都可以让您的代码正常工作,但您需要确保将“collectionName”设置为您已设置的 Cosmos DB 集合的正确名称。

例如,尝试将 Function.json 更改为以下内容:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "documentDB",
      "name": "outputDocument",
      "databaseName": "mycosmosdb",
      "collectionName": "MyTable",
      "createIfNotExists": true,
      "connection": "mycostmosdb_DOCUMENTDB",
      "direction": "out"
    }
  ],
  "disabled": false
}

【讨论】:

    【解决方案2】:
    • 确保这是您现有收藏的价值: (你提到了 MyTable 但它说的是 tableOutput)

      "collectionName": "tableOutput",
      
    • 确保名称相同。集合名称区分大小写。 “mytable”和“myTable”是不同的集合

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      • 2021-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      相关资源
      最近更新 更多