gpt4 book ai didi

azure - 找不到任何在 Cosmos db 本地存储/Azure 门户中创建集合的选项

转载 作者:行者123 更新时间:2023-12-02 08:30:46 26 4
gpt4 key购买 nike

我正在使用 Azure Function,需要在 cosmosdbtrigger 属性内添加“集合名称”,但没有任何选项来创建任何集合,我只能创建容器,因此在本地运行代码时存储导致错误:

The listener for function 'Function1' was unable to start. [15-04-2020 22:39:01] The listener for function 'Function1' was unable to start. Microsoft.Azure.WebJobs.Extensions.CosmosDB: Either the source collection 'driverLocation' (in database 'pizza') or the lease collection 'leases' (in database 'pizza') does not exist. Both collections must exist before the listener starts. To automatically create the lease collection, set 'CreateLeaseCollectionIfNotExists' to 'true'. Microsoft.Azure.DocumentDB.Core: Message: {"Errors":["Resource Not Found"]}

下面是我正在使用的样板代码,我已经在 cosmos db 模拟器中创建了数据库和集合

public static class Function1
{
[FunctionName("Function1")]
public static void Run([CosmosDBTrigger(
databaseName: "pizza",
collectionName: "driverLocation",
ConnectionStringSetting = "pizzaConnection")] IReadOnlyList<Document> input,
ILogger log)
{
if (input != null && input.Count > 0)
{
log.LogInformation("Documents modified " + input.Count);
log.LogInformation("First document Id " + input[0].Id);
}
}
}

下面是我的 local.settings.json 文件

    {
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"pizzaConnection": "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
},
"Host": {
"LocalHttpPort": 7071,
"CORS": "http://localhost:3872",
"CORSCredentials": true
}
}

最佳答案

您需要创建一个租约集合(在 CosmosDB 中也称为容器,并在 Cosmos DB 触发器中为租约集合指定名称,如下所示:

public static class Function1
{
[FunctionName("Function1")]
public static void Run([CosmosDBTrigger(
databaseName: "pizza",
collectionName: "driverLocation",
LeaseCollectionName = "leases"
CreateLeaseCollectionIfNotExists: true,
ConnectionStringSetting = "pizzaConnection")] IReadOnlyList<Document> input,
ILogger log)
{
if (input != null && input.Count > 0)
{
log.LogInformation("Documents modified " + input.Count);
log.LogInformation("First document Id " + input[0].Id);
}
}
}

这应该为您创建一个租赁容器(如果它不存在)。要使用 Azure Cosmos DB 模拟器创建集合,您应该会看到以下屏幕,您可以在其中创建容器:

enter image description here

本质上,只需启动模拟器,将其打开,然后单击“新建集合”即可。填写表格来创建您的收藏,然后就可以开始了。

希望这对您有所帮助,如果您需要更多信息或有其他问题,请告诉我:)

关于azure - 找不到任何在 Cosmos db 本地存储/Azure 门户中创建集合的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61237709/

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