我想在 Azure 中实现以下场景。
模拟 IoT 设备向 IoT 中心发送一些消息,并将其存储在 CosmosDB 中。到目前为止,这件事正在发挥作用。我使用 Azure Function 来实现这一目标。
下一步是使用 CosmosDB 中的更改源触发器创建新的 Azure 函数,并将其推送到 Azure 事件中心。
我在门户中创建了适当的function.json
{
"bindings": [
{
"type": "eventHub",
"name": "outputEventHubMessage",
"connection": "dev-euwest_all_EVENTHUB",
"eventHubName": "forwardedmessages",
"direction": "out"
},
{
"type": "cosmosDBTrigger",
"name": "inputDocuments",
"connectionStringSetting": "cdb-euwest_DOCUMENTDB",
"databaseName": "iot",
"collectionName": "messages",
"leaseCollectionName": "forwardedmessages",
"createLeaseCollectionIfNotExists": true,
"direction": "in"
}
],
"disabled": false
}
然后我有index.js
module.exports = async function (context, documents) {
if (!!documents && documents.length > 0) {
context.log('Document Id: ', documents[0].id);
}
}
当我运行它时,没有失败,但我没有看到任何文档 ID。
这是我使用 Azure IoT Python 设备 Azure Python Simulator 的示例源消息
{
"_id" : ObjectId("5da47cd2b19c571e106a040c"),
"temperature" : 34.78470937343868,
"humidity" : 79.96610016503291
}
我在这里做错了什么?如何正确拾取消息?
最佳答案
示例文档是一个 Bson 文档。这意味着您正在使用带有 Mongo API 的 Cosmos DB 帐户。
如果正确,请检查official documentation ,其中指出:
Currently, the Azure Functions trigger for Cosmos DB is supported for use with the Core (SQL) API only.
此外,在Azure Functions Binding documentation中,它指出:
Azure Cosmos DB bindings are only supported for use with the SQL API. For all other Azure Cosmos DB APIs, you should access the database from your function by using the static client for your API, including Azure Cosmos DB's API for MongoDB, Cassandra API, Gremlin API, and Table API.
目前触发器不支持Mongo。
关于javascript - 来自 CosmosDB JavaScript 的 Azure 函数更改源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58394359/