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

时间:2018-12-09 09:27:17

标签: c# azure azure-functions

我正在尝试使用Service Bus Queue触发器来触发azure功能,效果很好。我还想做的是在同一函数上使用Cosmos Db输入绑定。 该函数由特定文档触发,并通过输入绑定获得结果,以进行简单查询,例如:

Select * from c

但是使用WHERE子句,尽管条件正确,并且数据库中存在针对触发器触发的合同ID的数据,但相同的查询不会返回任何内容:

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

以下是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

 {
  "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}"
    }
  ]
}

触发数据进入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}}

1 个答案:

答案 0 :(得分:0)

似乎您的代码与function.json不匹配。若要将占位符设置为{contractId},我们需要定义一个自定义类型以反序列化JSON,以便功能代码可以在即将到来的数据中找到contractId

尝试下面的代码。

#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; }
}