简体   繁体   English

Azure函数-Cosmos数据库输入绑定查询不起作用

[英]Azure Functions - Cosmos db input binding query not working

I am trying to trigger azure function using Service Bus Queue trigger, which works fine. 我正在尝试使用Service Bus Queue触发器来触发azure函数,效果很好。 What I also want to do is use Cosmos Db input binding on the same function. 我还想做的是在同一函数上使用Cosmos Db输入绑定。 The function gets triggered with the specific document and gets result with the input binding for simple query like: 该函数通过特定文档触发,并通过输入绑定获取结果,以进行简单查询,例如:

Select * from c

But with a WHERE clause the same query does not return anything, though the condition is right and data is there in the DB against the contract Id passed in from the trigger: 但是使用WHERE子句,尽管条件正确,并且数据库中存在针对触发器触发的合同ID的数据,但相同的查询不会返回任何内容:

Select * from c WHERE c.contractId = {contractId}

Following is the code of the Azure Function 以下是Azure函数的代码

#r "Microsoft.Azure.DocumentDB.Core"
using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;

public static void Run(IReadOnlyList<Document> input, ILogger log, IEnumerable<dynamic> documents)
{
    if (input != null && input.Count > 0)
    {
        log.LogInformation("Documents modified " + input.Count);
        log.LogInformation("First document Id " + input[0]);
    }
}

function.json function.json

 {
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "serviceBusTrigger",
      "direction": "in",
      "queueName": "tripend",
      "connection": "mobiiot_RootManageSharedAccessKey_SERVICEBUS"
    },
    {
      "type": "cosmosDB",
      "name": "documents",
      "databaseName": "ToDoList",
      "collectionName": "Items",
      "connectionStringSetting": "mobiiot_DOCUMENTDB",
      "direction": "in",
      "sqlQuery": "SELECT * from c where c.contractId= {contractId}"
    }
  ]
}

Trigger Data coming into Azure Function: 触发数据进入Azure功能:

    {"vin":"WP0ZZZ99ZJS167001","milage":780.3333,"contractId":"19277",
"lat":51.47404,"lon":-0.45299000000000006,"noOfHardBreaks":0,"fuelConsumptionRate":22,
"speed":96,"status":"droppedOff","EventProcessedUtcTime":"2018-12-10T09:14:51.6474889Z",
"PartitionId":0,"EventEnqueuedUtcTime":"2018-12-10T09:14:51.5350000Z",
"IoTHub":{"MessageId":null,"CorrelationId":null,"ConnectionDeviceId":"WP0ZZZ99ZJS167001",
"ConnectionDeviceGenerationId":"636795108399273130",
"EnqueuedTime":"2018-12-10T09:14:51.5470000Z","StreamId":null}}

It seems your code doesn't match function.json . 似乎您的代码与function.json不匹配。 To set placeholder like {contractId} , we need to define a custom type to deserialize the JSON, so that function code could find contractId among the coming data. 若要将占位符设置为{contractId} ,我们需要定义一个自定义类型以反序列化JSON,以便功能代码可以在即将到来的数据中找到contractId

Have a try at code below. 请尝试以下代码。

#r "Microsoft.Azure.DocumentDB.Core"
using System;
using System.Collections.Generic;
using Microsoft.Azure.Documents;

public static void  Run(QueueItem myQueueItem, ILogger log, IEnumerable<dynamic> documents)
{      
    foreach(var doc in documents)
    {
        log.LogInformation((string)doc.id);
    }
}
public class QueueItem
{
    public string contractId { get; set; }
}

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

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