Azure functions 关于HHTPTrigger从CosmosDB检索文档
我正在尝试执行HTTP触发器azure函数以从CosmosDB检索文档。我试图在URL中传递id值,但无法检索文档。我从docs.microsoft.com获得了此示例代码 功能代码: function.json 网址:Azure functions 关于HHTPTrigger从CosmosDB检索文档,azure-functions,Azure Functions,我正在尝试执行HTTP触发器azure函数以从CosmosDB检索文档。我试图在URL中传递id值,但无法检索文档。我从docs.microsoft.com获得了此示例代码 功能代码: function.json 网址: 即使我在cosmosdb数据库中有id值为“3”的文档,函数也无法检索该文档。请指导我修复错误。我认为问题来自您的cosmosdb中的项目 仅提供id无法获取指定的项。容器中的同一id中可以存在多个项 请看一下我的cosmosdb: 这是我的azure功能: init.py:
即使我在cosmosdb数据库中有id值为“3”的文档,函数也无法检索该文档。请指导我修复错误。我认为问题来自您的cosmosdb中的项目 仅提供id无法获取指定的项。容器中的同一id中可以存在多个项 请看一下我的cosmosdb: 这是我的azure功能: init.py:
import logging
import azure.functions as func
def main(req: func.HttpRequest, documents: func.DocumentList) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
if not documents:
logging.warning("ToDo item not found")
else:
logging.info("Found ToDo item")
return 'OK'
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
},
{
"name": "documents",
"type": "cosmosDB",
"databaseName": "testbowman",
"collectionName": "testbowman",
"id" : "1",
"partitionKey": "1",
"connectionStringSetting": "MyAccount_COSMOSDB",
"direction": "in"
}
]
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"MyAccount_COSMOSDB":"AccountEndpoint=https://testbowman.documents.azure.com:443/;AccountKey=xxxxxx;"
}
}
function.json:
import logging
import azure.functions as func
def main(req: func.HttpRequest, documents: func.DocumentList) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
if not documents:
logging.warning("ToDo item not found")
else:
logging.info("Found ToDo item")
return 'OK'
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
},
{
"name": "documents",
"type": "cosmosDB",
"databaseName": "testbowman",
"collectionName": "testbowman",
"id" : "1",
"partitionKey": "1",
"connectionStringSetting": "MyAccount_COSMOSDB",
"direction": "in"
}
]
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"MyAccount_COSMOSDB":"AccountEndpoint=https://testbowman.documents.azure.com:443/;AccountKey=xxxxxx;"
}
}
local.settings.json:
import logging
import azure.functions as func
def main(req: func.HttpRequest, documents: func.DocumentList) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
if not documents:
logging.warning("ToDo item not found")
else:
logging.info("Found ToDo item")
return 'OK'
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
},
{
"name": "documents",
"type": "cosmosDB",
"databaseName": "testbowman",
"collectionName": "testbowman",
"id" : "1",
"partitionKey": "1",
"connectionStringSetting": "MyAccount_COSMOSDB",
"direction": "in"
}
]
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"MyAccount_COSMOSDB":"AccountEndpoint=https://testbowman.documents.azure.com:443/;AccountKey=xxxxxx;"
}
}
启动函数后,只能捕获以下类似项:
{
"id": "1",
"testbowman": "1"
}
(这是因为我必须在提供id的同时提供分区键的值。在我的例子中,分区键的名称是testbowman。)
实际使用时,不需要将值固定为1,可以通过{}
指定相应参数的值
你可以看到我可以成功地到达这里:
我认为问题来自您的cosmosdb中的项目 仅提供id无法获取指定的项。容器中的同一id中可以存在多个项 请看一下我的cosmosdb: 这是我的azure功能: init.py:
import logging
import azure.functions as func
def main(req: func.HttpRequest, documents: func.DocumentList) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
if not documents:
logging.warning("ToDo item not found")
else:
logging.info("Found ToDo item")
return 'OK'
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
},
{
"name": "documents",
"type": "cosmosDB",
"databaseName": "testbowman",
"collectionName": "testbowman",
"id" : "1",
"partitionKey": "1",
"connectionStringSetting": "MyAccount_COSMOSDB",
"direction": "in"
}
]
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"MyAccount_COSMOSDB":"AccountEndpoint=https://testbowman.documents.azure.com:443/;AccountKey=xxxxxx;"
}
}
function.json:
import logging
import azure.functions as func
def main(req: func.HttpRequest, documents: func.DocumentList) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
if not documents:
logging.warning("ToDo item not found")
else:
logging.info("Found ToDo item")
return 'OK'
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
},
{
"name": "documents",
"type": "cosmosDB",
"databaseName": "testbowman",
"collectionName": "testbowman",
"id" : "1",
"partitionKey": "1",
"connectionStringSetting": "MyAccount_COSMOSDB",
"direction": "in"
}
]
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"MyAccount_COSMOSDB":"AccountEndpoint=https://testbowman.documents.azure.com:443/;AccountKey=xxxxxx;"
}
}
local.settings.json:
import logging
import azure.functions as func
def main(req: func.HttpRequest, documents: func.DocumentList) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
if not documents:
logging.warning("ToDo item not found")
else:
logging.info("Found ToDo item")
return 'OK'
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
},
{
"name": "documents",
"type": "cosmosDB",
"databaseName": "testbowman",
"collectionName": "testbowman",
"id" : "1",
"partitionKey": "1",
"connectionStringSetting": "MyAccount_COSMOSDB",
"direction": "in"
}
]
}
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"MyAccount_COSMOSDB":"AccountEndpoint=https://testbowman.documents.azure.com:443/;AccountKey=xxxxxx;"
}
}
启动函数后,只能捕获以下类似项:
{
"id": "1",
"testbowman": "1"
}
(这是因为我必须在提供id的同时提供分区键的值。在我的例子中,分区键的名称是testbowman。)
实际使用时,不需要将值固定为1,可以通过{}
指定相应参数的值
你可以看到我可以成功地到达这里:
如果我没记错的话,cosmosdb只提供id是不够的,还必须提供分区键。我稍后会提供答案。我已经发布了答案。请告诉我你是否能解决这个问题。非常感谢鲍曼。正如您所建议的,我已经包括了分区键和id,并且工作正常。它解决了我的问题。嗨,当一个人发布的答案解决了你的问题时,你应该结束这个问题。因为只有你这样做,其他人在面对类似问题和搜索时会知道这个问题已经有了答案,这将有助于其他人。我已经更新了勾号。谢谢如果我没记错的话,cosmosdb只提供id是不够的,还必须提供分区键。我稍后会提供答案。我已经发布了答案。请告诉我你是否能解决这个问题。非常感谢鲍曼。正如您所建议的,我已经包括了分区键和id,并且工作正常。它解决了我的问题。嗨,当一个人发布的答案解决了你的问题时,你应该结束这个问题。因为只有你这样做,其他人在面对类似问题和搜索时会知道这个问题已经有了答案,这将有助于其他人。我已经更新了勾号。谢谢