Azure Functions 中使用 DocumentDB

3

我正在尝试使用Azure Functions连接Azure DocumentDB并保存文档,但我不知道如何创建连接。


你在Azure Function中尝试了什么?请编辑您的问题并提供更多信息。目前您的问题并不清楚您遇到了什么困难。 - David Makogon
目前我已经创建了数据库并创建了 Azure 函数,但我不知道如何连接到数据库以列出、更新、创建等操作。 - Luís Fura
这与在ASPNET应用程序、控制台应用程序等上执行的方式相同。您可以在我的repo中找到一个示例。 - Jose Roberto Araujo
5个回答

4

您可以使用Azure门户完成此操作。 在创建了DocumentDB之后 -

  • 创建新的Azure功能。
  • 转到“集成”选项卡。
  • 您可以选择将Azure Document DB作为函数的输出。
  • 选择要使用的Document DB /数据库名称/集合。
  • 文档参数名称是您函数的输出。

例如

using System;

public static void Run(string input, out object document, TraceWriter log)
{
    log.Info($"C# manually triggered function called with input: {input}");
    document = new {
        text = $"I'm running in a C# function! {input}"
    };
}

你需要提供一个输出对象,该对象与你在“输出”选项卡中定义的对象相同。

我该如何连接数据库并获取信息? - Luís Fura
@LuísFura 如果你想查询文档数据库,有很多在线指南可供参考。例如 - https://azure.microsoft.com/zh-cn/documentation/articles/documentdb-get-started/ - shachar
我尝试添加 azure.documents 的引用,但是出现了错误消息。在哪里可以添加引用到我的函数中? - Luís Fura
这里有一个指南,教你如何添加对Azure Functions的引用 - https://azure.microsoft.com/zh-cn/documentation/articles/functions-reference-csharp/ 另外,还可以从SO上获取更多信息 - https://dev59.com/uFoV5IYBdhLWcg3wTdXN - shachar
最近,我刚回答过一个类似的问题,关于在脚本中如何访问DocumentClient:https://social.msdn.microsoft.com/Forums/azure/en-US/0d315697-f605-4489-ba0c-ab940b0612dc/azure-functions-with-azure-documentdb?forum=AzureFunctions - brettsam
与Azure离线同步和Azure SQL表相比,这是更好的选择吗?成本可以降低,性能可以提高吗? - Emil

1
Azure Functions支持文档数据库(Cosmos DB),您只需在V1中添加名为的环境变量,或在V2中添加名为的环境变量。然后,只需使用CosmosDBTrigger绑定属性进行输入绑定,例如(以C#为例):
public static class UpsertProductCosmosDbTrigger
{
    [FunctionName("ProductUpsertCosmosDbTrigger")]
    public static void Run(
        [CosmosDBTrigger(
        // Those names come from the application settings.
        // Those names can come with both preceding % and trailing %.
        databaseName: "CosmosDbDdatabaseName",
        collectionName: "CosmosDbCollectionName",
        LeaseDatabaseName = "CosmosDbDdatabaseName",
        LeaseCollectionName = "CosmosDbLeaseCollectionName")] 
        IReadOnlyList<Document> input,

        TraceWriter log)
...

对于输出绑定,请在 V1 中使用 DocumentDB 输出绑定属性,在 V2 中使用 CosmosDB,如下所示:

[FunctionName("ProductUpsertHttpTrigger")]
public static async Task<HttpResponseMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, "post", Route = "products")] 
    HttpRequestMessage req,

    [DocumentDB(
    databaseName: "%CosmosDbDdatabaseName%",
    collectionName: "%CosmosDbCollectionName%")] IAsyncCollector<Product> collector,

    TraceWriter log)
...

我已经写了一篇关于此的博客文章:https://blog.mexia.com.au/cosmos-db-in-azure-functions-v1-and-v2

1
您可以直接使用文档客户端:
var endpoint = "https://XXXXX.documents.azure.com:443/";
var authKey = "XXXXX";

using (var client = new DocumentClient(new Uri(endpoint), authKey))
{
    var sqlCountQuery = "select value count(1) from c";
    IDocumentQuery<dynamic> query = client.CreateDocumentQuery<dynamic>(UriFactory.CreateDocumentCollectionUri("YOUR_DB_ID", "YOUR_COLLECTON_ID"), sqlCountQuery).AsDocumentQuery();
    ....
}

0
var EndpointUrl = "EndpointUrl";
var PrimaryKey = "PrimaryKeyValue" 
this.client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);
Database database = await this.client.CreateDatabaseIfNotExistsAsync(new Database { Id = cosmoDbName });

您可以在 Azure 门户的密钥部分获取终结点 URL 和主键值。


0

假设C#有类似Java的SDK。以下是针对Java的内容:

从Azure函数连接到documentDB有两种方法。

  1. 使用SDK

    DocumentClient documentClient = new DocumentClient( "SERVICE_ENDPOINT", "MASTER_KEY", ConnectionPolicy.GetDefault(), ConsistencyLevel.Session);

参考 - [https://learn.microsoft.com/en-us/azure/cosmos-db/sql-api-java-samples][1]。这里也有.Net示例。

  1. 绑定

    @FunctionName("CosmosDBStore") @CosmosDBOutput(name = "database", databaseName = "db_name", collectionName = "col_name", connectionStringSetting = "AzureCosmosDBConnection")

请确保您的应用程序设置和local.settings.json(如果您想在本地测试)中都有一个名为"AzureCosmosDBConnection"的变量。

参考 - [https://learn.microsoft.com/zh-cn/azure/azure-functions/functions-bindings-cosmosdb-v2][1]

上述链接还包含C#示例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接