【问题标题】:How to retrieve data from Azure Cosmos Db with Core(SQL) API如何使用 Core(SQL) API 从 Azure Cosmos Db 检索数据
【发布时间】:2019-02-26 06:45:17
【问题描述】:

美好的一天,

我正在 Visual Studio 中使用 .net framework/V1 开发一个 azure 函数,我想从 Azure Function 上的 TestData 集合中检索 clientDetails 数据,我的函数是HttpTrigger。

我尝试了此链接中的示例:Azure Cosmos DB bindings for Azure Functions 1.x

我创建了两个属性类:

    public class DocumentClient
    {
        [JsonProperty("clientDetails")]
        public List<ClientDetails> clientDetails { get; set; }
    }

    public class ClientDetails
    {
        public string domain { get; set; }
        public string channel { get; set; }
        public string retailer { get; set; }
        public string store { get; set; }
        public string auth { get; set; }
    }

这里是构造函数:

public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function), Disable()]HttpRequestMessage req, [DocumentDB(
        databaseName: "Client",
        collectionName: "TestData",
        ConnectionStringSetting = "CosmosDBConnection"
       )] DocumentClient client, TraceWriter log)

但我在运行该函数时遇到错误Id is required,但每当我尝试添加时

Id ="{Hardcoded - the TestData's Id}"

触发函数时出现错误“必须为此操作提供PartitionKey值”

我在我的 local.settings.json 中添加了这个:

"CosmosDBConnection": "AccountEndpoint=https://test.com:443/;AccountKey=testkey==;"

问候

【问题讨论】:

  • 你是如何定义你的 cosmo db 架构的 >???

标签: c# azure azure-functions azure-cosmosdb


【解决方案1】:

我遇到错误“必须为此提供 PartitionKey 值 函数触发时的“操作”

根据此错误消息,我认为您需要在function.json 文件中设置partitionKey。请参考此supported properties list

PartitionKey 指定查找的分区键值。可能 包括绑定参数。

这是一个示例:

{
    "name": "inputDocument",
    "type": "documentDB",
    "databaseName": "MyDatabase",
    "collectionName": "MyCollection",
    "id" : "{queueTrigger}",
    "partitionKey": "{partition key value}",
    "connection": "MyAccount_COSMOSDB",
    "direction": "in"
}

【讨论】:

  • 我可以通过将它添加到 json "AzureWebJobsStorage" "AzureWebJobsDashboard" 来运行它,它的值是我的存储 acc 的连接字符串。我将构造函数更改为: IAsyncCollector client ,但我还无法看到客户端值。
  • @NicoTing 仍然遇到与分区键相关的相同错误?
  • 没有,但我看不到 clientDetails
猜你喜欢
  • 1970-01-01
  • 2021-10-03
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
  • 2019-04-10
  • 2023-02-21
  • 2019-06-01
  • 1970-01-01
相关资源
最近更新 更多