首页 文章

在Azure Functions中从Azure Service Bus接收JSON(Cosmos DB文档)作为输入

提问于
浏览
0

我有一个Azure功能(f1)将Cosmos DB文档作为输入,我想设置一个路径,将任何文档重定向到Azure服务总线队列,以防下游可能发生某种类型的故障(SQL语句到SQL Server )可能 . 我想在一个计时器上运行Azure函数(f2),该计时器将作为任何这些重定向文档的“清理”,并尝试执行基本上与f1尝试相同的操作 .

我已经能够将单个文档或多个文档发送到ServiceBus队列,但是当它是单个文档时,我只能在f2中成功处理来自队列的输入 . 请注意,f2是触发器,我仍然不确定如何从Azure函数中读取队列?如何在1个函数中完成迭代整个消息队列?绑定显示ServiceBus不是Azure函数的有效"Input",尽管它作为触发器有效 . https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings

到目前为止,我正在使用以下内容:

f1具有单个输出

#r "Microsoft.Azure.Documents.Client"
#r "Microsoft.ServiceBus"
using Microsoft.ServiceBus.Messaging;
using Microsoft.Azure.Documents;
using System.Collections.Generic;
using System;

public static void Run(IReadOnlyList<Document> inputFeed, TraceWriter log,
    out Document outputSbQueue)
{
  //f1 with a single output to a Queue
}

f1带有ICollector输出

#r "Microsoft.Azure.Documents.Client"
#r "Microsoft.ServiceBus"
using Microsoft.ServiceBus.Messaging;
using Microsoft.Azure.Documents;
using System.Collections.Generic;
using System;

public static void Run(IReadOnlyList<Document> inputFeed, TraceWriter log,
    ICollector<Document> outputSbQueue)
{
  //f1 with an ICollector output
}

f2与ServiceBus触发器的单个输入(工作)

#r "Microsoft.Azure.Documents.Client"
#r "Newtonsoft.Json"
using Microsoft.Azure.Documents;
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
public static void Run(Document myQueueItem, TraceWriter log)
{
    //f2 with a working single input -- myQueueItem is an SB trigger
}

f2与ICollector或任何类型的IEnumerable输入(不起作用...在运行时抛出有关JSon序列化的异常)

#r "Microsoft.Azure.Documents.Client"
#r "Newtonsoft.Json"
using Microsoft.Azure.Documents;
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;

public static void Run(IReadOnlyList<Document> myQueueItem, TraceWriter log)
{
    //f2 with an input of more than one document
}

抛出此异常:

2018-05-15T20:48:21.535 [错误]执行函数时出现异常:Functions.ServiceBusQueueTriggerCSharp1 . Microsoft.Azure.WebJobs.Host:异常绑定参数'myQueueItem' . Microsoft.Azure.WebJobs.ServiceBus:将参数绑定到复杂对象(例如'ICollector1')使用Json.NET序列化 . 1.将参数类型绑定为'string'而不是'ICollector1'以获取原始值并避免JSON反序列化,或2.将队列有效负载更改为有效的json . JSON解析器失败:无法将当前JSON数组(例如[1,2,3])反序列化为类型'Microsoft.Azure.WebJobs.ICollector`1 [Microsoft.Azure.Documents.Document]',因为该类型需要JSON对象(例如{“name”:“value”})正确反序列化 . 要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList),例如List从JSON数组反序列化 . JsonArrayAttribute也可以添加到类型中以强制它从JSON数组反序列化 . 路径'',第1行,第1位 .

我是处理JSON,序列化和反序列化的完全新手 . 我只是想解析它,从中读取内容并获得准备发送到下游的SQL语句 .

1 回答

  • 0

    目前,Service Bus Trigger不支持批处理(多个项目作为输入) . 请参阅Service Bus Batch Trigger github问题 .

    如果我正确理解您的场景,您不一定要接受 f2 函数中的批处理,也不需要"iterate through the entire Queue of messages" .

    迭代将由函数处理 . 您的 f2 函数将被调用多次 f1 发送到Service Bus队列的消息 . 如果您在获得文档时逐个"clean up"文档,您最终将清理所有文档 .

相关问题