无法从Azure事件中心写入CosmosDB

问题描述 投票:1回答:1

我正在使用Azure函数(2.x),该函数由来自事件中心的事件触发,并将接收到的数据写入CosmosDB实例。在部署它之前,我想在本地对其进行测试,并且阅读该事件的过程非常完美。但是,当我尝试写入CosmosDB时,出现此错误:

“ System.Private.CoreLib:执行函数时发生异常:Functions.EventHubTrigger。Microsoft.Azure.DocumentDB.Core:消息:{”错误“:[”指定的输入之一无效“]}”

该数据库实例是使用Azure门户创建的,我添加了两个虚拟条目,所有这些条目都可以正常工作。我在做什么错?

function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "event",
      "direction": "in",
      "eventHubName": "event-hub-name",
      "connection": "event-hub-connection",
      "cardinality": "many",
      "consumerGroup": "$Default"
    },
    {
      "type": "cosmosDB",
      "direction": "out",
      "name": "doc",
      "databaseName": "database-name",
      "collectionName": "test",
      "createIfNotExists": "true",
      "connectionStringSetting": "CosmosDBConnectionString"
    }
  ]
}

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "storage-key",
    "CosmosDBConnectionString": "AccountEndpoint=document-db-endpoint;AccountKey=account-key;",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "event-hub-connection": "Endpoint=sb://endpoint-name;SharedAccessKeyName=shared-access-key-name;SharedAccessKey=shared-access-key" 
  }
}

host.json

{
  "version": "2.0",
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[1.*, 2.0.0)"
  }
}

__init__.py

import logging, json

import azure.functions as func


def main(event: func.EventHubEvent, doc: func.Out[func.Document]):

    event_data = event.get_body().decode("utf-8")
    logging.info('Python EventHub trigger processed an event: %s', event_data)

    # json object for testing the DB write operation
    temp = {}
    temp["id"] = 1
    temp["category"] = "feedback"
    temp = json.dumps(temp)

    doc.set(func.Document.from_json(temp))
    logging.info("CosmosDB updated!; Value: ", temp)
python azure azure-functions azure-cosmosdb azure-eventhub
1个回答
0
投票

该错误是HTTP 400 BadRequest。意味着有效负载中的某些内容未正确形成JSON或您的某些属性无效。

我看到您的ID是一个数字,但是在REST合同中,它是一个字符串。参考:https://docs.microsoft.com/rest/api/cosmos-db/create-a-document#body

您可以将ID更改为字符串吗?

© www.soinside.com 2019 - 2024. All rights reserved.