Azure functions 在python中,如何使用function.json中的信息从azure无服务器函数写入cosmosdb?

Azure functions 在python中,如何使用function.json中的信息从azure无服务器函数写入cosmosdb?,azure-functions,azure-cosmosdb,Azure Functions,Azure Cosmosdb,我有一个azure函数,它由httprequest提供,应该使用CoreSQL将给定的输入存储到cosmos集合中。代码从中的请求获取数据,但数据从未写入数据库 通过阅读,我知道需要在function.json文件中定义从函数向外的连接。我的问题是,我需要使用function.json中的值来建立数据库连接吗 如果我这样做,我是否从azure.functions.Out对象获取它们?如果是这样,那么在给定function.json的“connection”字符串的情况下,如何连接到db,该字符串

我有一个azure函数,它由httprequest提供,应该使用CoreSQL将给定的输入存储到cosmos集合中。代码从中的请求获取数据,但数据从未写入数据库

通过阅读,我知道需要在function.json文件中定义从函数向外的连接。我的问题是,我需要使用function.json中的值来建立数据库连接吗

如果我这样做,我是否从azure.functions.Out对象获取它们?如果是这样,那么在给定function.json的“connection”字符串的情况下,如何连接到db,该字符串似乎是db的“PRIMARY connection string”。我是否应该拆分连接字符串以获取url和密钥

或者,我是否可以使用函数的“应用程序设置”来传递值,如果是,它们是否在函数的环境(os.getenv('xyz'))中

感谢您的指导,尤其是在Python中。

函数从Azure Cosmos DB触发器获取文档数据,并使用Cosmos DB输出绑定将数据存储到Azure Cosmos DB中

functions.json

{
“脚本文件”:“\uuuu init\uuuuu.py”,
“绑定”:[
{
“类型”:“cosmosDBTrigger”,
“名称”:“文件”,
“方向”:“在”,
“租赁集合名称”:“租赁”,
“ConnectionString设置”:“MyCosmosDBConnectionString”,
“databaseName”:“testdb”,
“collectionName”:“testcol01”,
“createLeaseCollectionIfNotExists”:true
},
{
“方向”:“输出”,
“类型”:“cosmosDB”,
“名称”:“outdoc”,
“databaseName”:“testdb”,
“collectionName”:“testcol02”,
“租赁集合名称”:“租赁”,
“createLeaseCollectionIfNotExists”:true,
“ConnectionString设置”:“MyCosmosDBConnectionString”,
“createIfNotExists”:true
}
]
}
函数从Azure Cosmos DB触发器获取文档数据,并使用Cosmos DB输出绑定将数据存储到Azure Cosmos DB中

functions.json

{
“脚本文件”:“\uuuu init\uuuuu.py”,
“绑定”:[
{
“类型”:“cosmosDBTrigger”,
“名称”:“文件”,
“方向”:“在”,
“租赁集合名称”:“租赁”,
“ConnectionString设置”:“MyCosmosDBConnectionString”,
“databaseName”:“testdb”,
“collectionName”:“testcol01”,
“createLeaseCollectionIfNotExists”:true
},
{
“方向”:“输出”,
“类型”:“cosmosDB”,
“名称”:“outdoc”,
“databaseName”:“testdb”,
“collectionName”:“testcol02”,
“租赁集合名称”:“租赁”,
“createLeaseCollectionIfNotExists”:true,
“ConnectionString设置”:“MyCosmosDBConnectionString”,
“createIfNotExists”:true
}
]

}
我找到了答案。在我的azure函数的定义中,这是我的function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "cosmos",
      "type": "cosmosDB",
      "direction": "out",
      "ConnectionStringSetting": "CosmosDBConnection",
      "databaseName": "messages",
      "collectionName": "messages_1",
      "createIfNotExists": true
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return",
      "dataType": "string" 
    }
  ]
}
它描述了三个绑定

  • 第一个是输入http触发器
  • 我与宇宙数据库的连接,在哪里

    • cosmos
      是名称
    • 方向是
      out
    • ConnectionStringSettings
      指向CosmosDBConnection,它被定义为函数的连接->应用程序设置。应用程序设置包含CosmosDB主连接字符串
      AccountEndpoint=https://....
    • 数据库
      是它将写入的数据库
    • collectionName
      是数据库中集合的名称
  • 用于返回http响应的http输出连接器

  • 在python代码中,我使用
    out
    作为传入并写入的参数

    import azure.functions as func
    from  azure.cosmos import cosmos_client
    
    
    def main(req: func.HttpRequest, cosmos: func.Out[func.Document]) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
    
        # your magic
    
        # create a dictionary, foo_bar in this case
        # On the parameter passed in, named cosmos, use the set() operator
        # in insert a Document, built from the dictionary
    
        cosmos.set(func.Document.from_dict(foo_bar))
    
    不需要对数据库进行其他设置。它由azure处理,处理function.json文件。只需确保function.json的ConnectionString设置指向保存连接字符串的属性

    还有一些花边新闻

    • 观察您的虚拟网络设置,以便运行并创建数据库
    • 使用Microsoft“func”软件包进行本地测试
    • 邮递员是本地测试的好工具

      • 我找到了答案。在我的azure函数的定义中,这是我的function.json

        {
          "scriptFile": "__init__.py",
          "bindings": [
            {
              "authLevel": "anonymous",
              "type": "httpTrigger",
              "direction": "in",
              "name": "req",
              "methods": [
                "get",
                "post"
              ]
            },
            {
              "name": "cosmos",
              "type": "cosmosDB",
              "direction": "out",
              "ConnectionStringSetting": "CosmosDBConnection",
              "databaseName": "messages",
              "collectionName": "messages_1",
              "createIfNotExists": true
            },
            {
              "type": "http",
              "direction": "out",
              "name": "$return",
              "dataType": "string" 
            }
          ]
        }
        
        它描述了三个绑定

      • 第一个是输入http触发器
      • 我与宇宙数据库的连接,在哪里

        • cosmos
          是名称
        • 方向是
          out
        • ConnectionStringSettings
          指向CosmosDBConnection,它被定义为函数的连接->应用程序设置。应用程序设置包含CosmosDB主连接字符串
          AccountEndpoint=https://....
        • 数据库
          是它将写入的数据库
        • collectionName
          是数据库中集合的名称
      • 用于返回http响应的http输出连接器

      • 在python代码中,我使用
        out
        作为传入并写入的参数

        import azure.functions as func
        from  azure.cosmos import cosmos_client
        
        
        def main(req: func.HttpRequest, cosmos: func.Out[func.Document]) -> func.HttpResponse:
            logging.info('Python HTTP trigger function processed a request.')
        
            # your magic
        
            # create a dictionary, foo_bar in this case
            # On the parameter passed in, named cosmos, use the set() operator
            # in insert a Document, built from the dictionary
        
            cosmos.set(func.Document.from_dict(foo_bar))
        
        不需要对数据库进行其他设置。它由azure处理,处理function.json文件。只需确保function.json的ConnectionString设置指向保存连接字符串的属性

        还有一些花边新闻

        • 观察您的虚拟网络设置,以便运行并创建数据库
        • 使用Microsoft“func”软件包进行本地测试
        • 邮递员是本地测试的好工具