首页 文章

Azure函数 - Cosmos db输入绑定查询无效

提问于
浏览
0

我试图使用Service Bus Queue触发器触发azure功能,这很好 . 我还想做的是在同一个函数上使用Cosmos Db输入绑定 . 该函数由特定文档触发,并通过输入绑定获取结果,用于简单查询,如:

Select * from c

但是使用WHERE子句时,相同的查询不会返回任何内容,尽管条件是正确的,并且数据库中的数据是针对从触发器传入的 Contract 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

    看来您的代码与 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; }
    }
    

相关问题