gpt4 book ai didi

python - 无法从 Azure 事件中心写入 CosmosDB

转载 作者:行者123 更新时间:2023-12-01 06:22:20 24 4
gpt4 key购买 nike

我正在开发一个 Azure Function(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)

最佳答案

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

我看到你的 id 是一个数字,但在 REST 合约中,它是一个字符串。引用:https://learn.microsoft.com/rest/api/cosmos-db/create-a-document#body

你能把id改成字符串吗?

关于python - 无法从 Azure 事件中心写入 CosmosDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60302169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com