Azure Function的CosmosDB输入绑定未使用HttpTrigger中定义的路径变量。

huangapple go评论70阅读模式
英文:

CosmosDBInput binding of Azure Function is not using path Variable defined in HttpTrigger

问题

我使用 azure-functions-kotlin-archetype 创建了一个 Kotlin 函数。我创建了一个 Http 触发器和一个 cosmos 输入绑定,用于从 Cosmos 中读取数据。我还在查询中指定了 SQL 查询,以获取数据。我想将路径变量传递给 Http 触发器,该变量应该在查询中用作参数。
根据 Microsoft 的文档,我已经相应地定义了路径参数。链接如下:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-input?tabs=java#http-trigger-get-multiple-docs-from-route-data-using-sqlquery-java

但是我得到了空白的响应,也就是没有获取到数据。如果我在 SQL 查询中硬编码参数,我可以成功获取数据。有没有人可以告诉我问题出在哪里。以下是函数代码:

@FunctionName("GetData")
fun run(
    @HttpTrigger(
        name = "req",
        methods = [HttpMethod.GET],
        authLevel = AuthorizationLevel.ANONYMOUS,
        route = "/api/getItems/{id}/{qnt}"
    )
    request: HttpRequestMessage<Optional<String>>,
    @CosmosDBInput(
        name = "cosmosdb",
        databaseName = "item-db",
        collectionName = "item",
        sqlQuery = "SELECT * FROM ITEM n where n.id= {id} "
                   + "and n.qnt = {qnt}",
        connectionStringSetting = "Cosmos_DB_Connection_String"
    )
    rs: Array<String>,
    context: ExecutionContext
): HttpResponseMessage {

    return request
        .createResponseBuilder(HttpStatus.OK)
        .body(rs)
        .build()
}

请注意,我已经将代码中的 HTML 实体编码解码,以便更清楚地呈现内容。

英文:

I have created a function with Kotlin using azure-functions-kotlin-archetype. I have created a Http Trigger and a cosmos input binding to read data from the cosmos. I have mentioned sql query also to fetch the data. I want to pass path variable to the http trigger which should be used as parameter in the query.
As per Microsoft documentation I have defined the path parameter accordingly.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-input?tabs=java#http-trigger-get-multiple-docs-from-route-data-using-sqlquery-java

But I am getting blank response, means no data is fetched. If I hard code the parameter in the Sql query, I able to fetch the data. Can any one please tell me the issue here. The Function Code is as below

@FunctionName(&quot;GetData&quot;)
    fun run(
            @HttpTrigger(
                    name = &quot;req&quot;,
                    methods = [HttpMethod.GET],
                    authLevel = AuthorizationLevel.ANONYMOUS,
			route = &quot;/api/getItems/{id}/{qnt}&quot;
			)
			request: HttpRequestMessage&lt;Optional&lt;String&gt;&gt;,
			@CosmosDBInput(
				name = &quot;cosmosdb&quot;,
				databaseName = &quot;item-db&quot;,
				collectionName = &quot;item&quot;,
				sqlQuery = &quot;SELECT * FROM ITEM n where n.id= {id} &quot;
                             +&quot;and n.qnt = {qnt}&quot;,
              connectionStringSetting = &quot;Cosmos_DB_Connection_String&quot;
			)
			rs: Array&lt;String&gt;,
            context: ExecutionContext): HttpResponseMessage {



            return request
                    .createResponseBuilder(HttpStatus.OK)
                    .body(rs)
                    .build()
        }

}

答案1

得分: 0

根据您提供的示例查询:SELECT * FROM ITEM n where n.id= {&#39;ID1&#39;} &quot; +&quot;and n.qnt = {1},似乎id是字符串,而qnt是一个数字。当我们从route获取参数时,它会将参数视为字符串。因此,如果您直接在sqlQuery中使用来自route的参数,它将选择如下的SQL数据:SELECT * FROM ITEM n where n.id = &#39;ID1&#39; and n.qnt = &#39;1&#39;。因此,不会获取任何数据。

您需要从route中获取参数qnt作为数字而不是字符串,因此请使用route = &quot;api/getItems/{id}/{qnt:int}&quot;

===========================更新=========================

为其他社区提供此更新参考:

在SQL查询中使用StringToNumber()方法,如sqlQuery = &quot;SELECT * FROM ITEM n where n.id= {id} &quot; + &quot;and n.qnt = StringToNumber({qnt})&quot;

英文:

According to the sample query you provided: SELECT * FROM ITEM n where n.id= {&#39;ID1&#39;} &quot; +&quot;and n.qnt = {1}, it seems the id is string and qnt is a number. When we get the parameters from route, it will treat the parameters as string. So if you use the parameters from route in sqlQuery directly, it will select the data like this sql: SELECT * FROM ITEM n where n.id = &#39;ID1&#39; and n.qnt = &#39;1&#39;. So no data is fetched.

You need to get the parameter qnt as a number but not as string from route, so please use route = &quot;api/getItems/{id}/{qnt:int}&quot;.

===========================Update=========================

Add this update for other communities reference:

Use the method StringToNumber() in sql query like sqlQuery = &quot;SELECT * FROM ITEM n where n.id= {id} &quot; + &quot;and n.qnt = StringToNumber({qnt})&quot;.

huangapple
  • 本文由 发表于 2020年8月10日 17:35:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63337640.html
  • azure-cosmosdb-sqlapi
  • azure-functions
  • azure-java-sdk
  • java
  • kotlin
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定