简体   繁体   English

是否可以使用变量从 function 中动态调用 SQL 查询 Azure 函数的 Cosmos DB 输入绑定?

[英]Can the SQL query for an Azure Function's Cosmos DB input binding be called dynamically, with variables, from within the function?

I have a collection of temperature values with timestamps in Azure Cosmos DB from which I want to query the average temperature value from a particular upload date.我在 Azure Cosmos DB 中有一组带有时间戳的温度值,我想从中查询特定上传日期的平均温度值。

I currently have a working solution using the @azure/cosmos client library, which allows me to dynamically build and execute queries against my temperature collection from an Azure Function.我目前有一个使用@azure/cosmos客户端库的工作解决方案,它允许我动态地构建和执行针对来自 Azure Function 的温度收集的查询。

I was wondering if it was possible to execute dynamic SQL queries against a Cosmos DB input binding, rather than using the cosmos client library, simply for performance benefits?我想知道是否可以对 Cosmos DB 输入绑定执行动态 SQL 查询,而不是使用 cosmos 客户端库,只是为了提高性能?

Also, if this is possible, would using the input binding actually afford a performance benefit?此外,如果这是可能的,那么使用输入绑定实际上会带来性能优势吗?

Well it depends on what you meant with dynamic.好吧,这取决于您对动态的含义。 If you meant the query changes based on input from your request( HttpTrigger ) then the answer is yes it can.如果您的意思是查询会根据您的请求( HttpTrigger )的输入而更改,那么答案是可以的。 Example:例子:

SELECT * FROM c WHERE c.temperature > {temp} AND (c.expired?? false) = false

Here {temp} is replaced with data in your request body eg.这里{temp}被您的请求正文中的数据替换,例如。

{
  "deviceId": "22dsa2d55f",
  "temp": 55.3665,
  "time": "2020-08-14T15:06:31.277Z"
}

As far as I'm aware there is no performance benefit.据我所知,没有性能优势。 It is only advertised as reducing code complexity by eliminating the need for the io code.它仅被宣传为通过消除对 io 代码的需要来降低代码复杂性。

You can't use dynamic filters on the input bindings.您不能在输入绑定上使用动态过滤器。 The input binding is used in the context of you want to trigger your function by the Cosmos DB (eg when a given document is updated or deleted or created).输入绑定用于您希望由 Cosmos DB 触发 function 的上下文(例如,当更新、删除或创建给定文档时)。 It is not meant to do filtering.它并不意味着进行过滤。 What you can do is apply your filtering logic on your function, to decide which documents you want to process and the ones to skip.您可以做的是在您的 function 上应用您的过滤逻辑,以决定您要处理哪些文档以及要跳过哪些文档。 In terms of filtering directly against Cosmos DB, you can still use the SDK or the API.在直接针对 Cosmos DB 进行过滤方面,您仍然可以使用 SDK 或 API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM