简体   繁体   English

无法从 Azure 事件中心写入 CosmosDB

[英]Unable to write to CosmosDB from an Azure Event Hub

I'm working on an Azure Function(2.x) which is triggered by events coming from an Event Hub and writes the received data to a CosmosDB instance.我正在开发一个 Azure Function(2.x),它由来自事件中心的事件触发,并将接收到的数据写入 CosmosDB 实例。 Before I deploy it, I'd like to test it locally and reading the event works flawlessly.在我部署它之前,我想在本地测试它并阅读事件完美无缺。 However, when I try to write to CosmosDB this error shows up:但是,当我尝试写入 CosmosDB 时,会出现此错误:

"System.Private.CoreLib: Exception while executing function: Functions.EventHubTrigger. Microsoft.Azure.DocumentDB.Core: Message: {"Errors":["One of the specified inputs is invalid"]}" “System.Private.CoreLib:执行函数时出现异常:Functions.EventHubTrigger。Microsoft.Azure.DocumentDB.Core:消息:{“错误”:[“指定的输入之一无效”]}”

The database instance was created using the Azure Portal and I added a couple of dummy entries, all of which works fine.数据库实例是使用 Azure 门户创建的,我添加了几个虚拟条目,所有这些都可以正常工作。 What am I doing wrong?我究竟做错了什么?

function.json : 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 : 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 : host.json

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

__init__.py : __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)

That error is a HTTP 400, BadRequest.该错误是 HTTP 400,BadRequest。 Meaning that something in the payload is not correctly formed JSON or some of your attributes is invalid.这意味着负载中的某些内容未正确形成 JSON 或您的某些属性无效。

I see your id is a number, but in the REST contract, it's a string.我看到您的 id 是一个数字,但在 REST 合同中,它是一个字符串。 Reference: https://docs.microsoft.com/rest/api/cosmos-db/create-a-document#body参考: https : //docs.microsoft.com/rest/api/cosmos-db/create-a-document#body

Can you change the id to a string?你能把id改成字符串吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM