针对Cosmos Db DocumentClient的Azure Function V2 [英] Azure Function V2 against Cosmos Db DocumentClient

查看:37
本文介绍了针对Cosmos Db DocumentClient的Azure Function V2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个Azure函数,该函数使用Azure函数的版本2针对Cosmos DB数据库返回数据.但是,我很难找到有关如何执行此操作的任何良好示例.我可以找到涉及搜索ID的非常基本的示例.

I need to write an Azure Function that returns data against a Cosmos DB Database using Version 2 of Azure Functions. However, I am having hard time finding any good examples on how to do this. I can find very basic examples that involve search on an id.

我希望能够向azure函数发送一些要查询的字段.例如在分区内和分区外的喜欢"和城市".我希望它以json文档的形式返回所有的所有记录.

I want to be able to send the azure function some fields to query on. Such as "Likes" and "City" with in a partition and outside a partition as well. I want it to return all the all the records as json documents.

Cosmos DB Json文档示例.

Example of Cosmos DB Json document.

{ "id": "46465464565455566546bddgd" "Name": "Scott Smith" "City": "Scottsdale" "_pk": "56"

{ "id": "46465464565455566546bddgd" "Name": "Scott Smith" "City": "Scottsdale" "_pk": "56"

到目前为止我的代码

`
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

using Newtonsoft.Json;

命名空间csharp { 公共静态类GetData {

namespace csharp { public static class GetData {

    private static readonly string CosmosDbApiKey = Environment.GetEnvironmentVariable("CosmosDbApiKey");
    private static readonly string CosmosDbUri = Environment.GetEnvironmentVariable("CosmosDbUri");
    private static readonly DocumentClient DocumentClient = new DocumentClient(new Uri(CosmosDbUri), CosmosDbApiKey);


    [FunctionName(nameof(GetData))]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "api/data/getdata/{city}{likes},{_pk}")]HttpRequest req,
        string city,
        string likes,
        string _pk,
        TraceWriter log)
    {

        IActionResult result;

        try
        {

            var Options = new RequestOptions() { PartitionKey = new PartitionKey(_pk) };
            var Sql = "SELECT * FROM c" WHERE c.name={name};

            var Uri = UriFactory.CreateDocumentCollectionUri("meddb", "medcol");
            var documentUri = DocumentClient.CreateDocumentQuery(Uri, Sql, Options);
            ResourceResponse<Document> document = await DocumentClient.ReadDocumentAsync(documentUri);


            result = new OkObjectResult(
                JsonConvert.SerializeObject(document.Resource, Formatting.Indented)
            );
        }
        catch (Exception e)
        {
            log.Error(e.Message, e);
            result = new BadRequestObjectResult(e);
        }

        return result;
    }
}
}
  `

非常感谢您的帮助!我遇到的问题是在尝试"部分之后.或者,如果有更好的方法可以做到这一点,我也很开放!

I would greatly appreciate any help! Where I am having is issues is after the "Try" section. Or if there is a better way of doing this I am open too!

谢谢!

推荐答案

将功能连接到Cosmos DB的标准方法是使用

The standard way to connect Functions to Cosmos DB is to use Azure Cosmos DB bindings. See that article for installation instructions.

您还可以从中获取DocumentClient的实例以执行自定义查询.在功能食谱中可以找到使用绑定的几个示例:

You can also get an instance of DocumentClient from it to execute custom queries. Several examples of using the binding can be found in Functions Recipes: Cosmos DB (DocumentDB) Bindings (they are for v1, so still DocumentDB).

您的代码如下:

[FunctionName(nameof(GetData))]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", Route = "api/data/getdata/{city}{likes},{_pk}")] HttpRequest req,
    [CosmosDB("test", "test", ConnectionStringSetting = "CosmosDB")] DocumentClient client, 
    string city,
    string likes,
    string _pk,
    TraceWriter log)

该方法的实现应该没有什么不同,并且它并不是真正针对Function的.

The implementation of the method shouldn't be different, and it's not really specific to Functions.

P.S.您没有提到任何特定的问题,但是如果您在编写正确的查询时遇到麻烦,请仅使用Cosmos DB代码并使用确切的问题声明,使用较小的代码示例提出一个新问题.

P.S. You haven't mentioned any specific problems, but if you have troubles writing proper query, make a new question with smaller code sample with Cosmos DB code only and exact problem statement.

这篇关于针对Cosmos Db DocumentClient的Azure Function V2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆