Azure Cosmos DB 输入绑定(bind) - 无法将 OFFSET 和 LIMIT 值作为参数传递?

标签 azure azure-functions azure-cosmosdb

我在尝试将 OFFSETLIMIT 的值作为查询参数动态传递到我的 CosmosDB 触发器时遇到问题。

如果我将这两个值硬编码到查询中,它就会按预期工作。

但是,使用以下代码:

 {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "route": "v1/query/properties",
      "methods": [
        "post"
      ]
    },
    {
      "name": "propertiesInquiryInput",
      "type": "cosmosDB",
      "databaseName": "property",
      "collectionName": "discovery",
      "connectionStringSetting": "CosmosDBConnectionString",
      "direction": "in",
      "leaseCollectionName": "leases",
      "sqlQuery": "SELECT * FROM c WHERE c.country={country} OFFSET {pageNo} LIMIT {perPage}"
    },

执行时出现以下失败:

System.Private.CoreLib: Exception while executing function: Functions.queryProperties. Microsoft.Azure.DocumentDB.Core: Message: {"errors":[{"severity":"Error","location":

{"start":48,"end":55},"code":"SC2062","message":"The OFFSET count value exceeds the maximum allowed value."},
{"severity":"Error","location":{"start":62,"end":70},"code":"SC2061","message":"The LIMIT count value exceeds the maximum allowed value."}]}

我对 Azure 服务及其模式相对较新,所以也许我在这里遗漏了一些明显的东西。我尝试通过 POST 将这些值作为 JSON 对象发送,并通过 GET 请求作为查询参数发送。似乎没有任何作用。

我也不知道有什么方法可以查看正在触发的 SQL 查询,以便从这个角度对其进行调试。任何正确方向的指导将不胜感激。

更新:

为了清晰起见,添加函数体:

module.exports = async function (context, req) {
    const results = context.bindings.propertiesInquiryInput;
    !results.length && context.done(null, { status: 404, body: "[]", });

    const body = JSON.stringify(results.map(data => reshapeResponse(data));

    return context.done(null, { status: 200, body });
}

最佳答案

除了原来的问题:

使用参数化查询和 python 的 azure.cosmos=4.2.0 pypi 包获得相同的异常消息。

重现案例:

query = f"""
SELECT *
FROM c
WHERE ...
AND c.run.submittedBy = @author
OFFSET 0 LIMIT @exp_limit
"""

items = list(cdb_container.query_items(
    query=query,
    parameters=[
        {"name":"@exp_limit", "value": f"{num_of_experiments}"}, # can't pass as param due to `The LIMIT count value exceeds the maximum allowed value.`
        {"name":"@author", "value": f"{submitter}"},
    ],
    enable_cross_partition_query=True
))

解决方法(使用查询的格式化字符串):

num_of_experiments = 10
query = f"""
SELECT *
FROM c
WHERE ...
AND c.run.submittedBy = @author
OFFSET 0 LIMIT {num_of_experiments}
"""

出于示例目的,在 WHERE 语句中省略了不相关的参数。可能对遇到类似情况的人有帮助。

关于Azure Cosmos DB 输入绑定(bind) - 无法将 OFFSET 和 LIMIT 值作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57254515/

相关文章:

azure-functions - Dotnet Isolated Azure Function w/.Net 6 在 Linux 消费计划上提示 6.0 AspNetCore.App Runtime 不可用

python - Azure 函数卡住(?)

c# - DateTime、Epoch 和 DocumentDb

Azure DocumentDb 扁平建模与嵌套建模

c# - RoleEnvironment.GetConfigurationSettingValue 每次都会从 cfg 文件中读取吗?

azure - CloudBlockBlob.UploadText 的行为是什么?

angularjs - 预检响应不成功

java - 如何从azure函数java中的POST请求中提取数据

azure - CosmosDb 搜索索引与分区键

azure - 如何使用 Postman 将查询发送到 Azure 表 REST API