gpt4 book ai didi

azure - 如何在 Azure Function 中写入 Azure 事件网格主题

转载 作者:行者123 更新时间:2023-12-02 07:35:22 25 4
gpt4 key购买 nike

我注意到,主题突然不再作为服务总线的一部分提供。是否可以将消息写入 Azure 函数内的事件网格主题?

最佳答案

以下代码片段是使用 azure 函数将遥测流推送到事件模型的示例:

enter image description here

#r "Microsoft.ServiceBus"
#r "Newtonsoft.Json"


using System.Configuration;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;
using Newtonsoft.Json;

// reusable client proxy
static HttpClient client = HttpClientHelper.Client(ConfigurationManager.AppSettings["TopicEndpointEventGrid"], ConfigurationManager.AppSettings["aeg-sas-key"]);

// AF
public static async Task Run(EventData ed, TraceWriter log)
{
log.Info($"C# Event Hub trigger function processed a message:{ed.SequenceNumber}");
//foreach(var prop in ed.SystemProperties)
// log.Info($"{prop.Key} = {prop.Value}");

// fire EventGrid Custom Topic
var egevent = new
{
Id = ed.SequenceNumber.ToString(),
Subject = $"/iothub/events/{ed.SystemProperties["iothub-message-source"] ?? "?"}/{ed.SystemProperties["iothub-connection-device-id"] ?? "?"}",
EventType = "telemetryDataInserted",
EventTime = ed.EnqueuedTimeUtc,
Data = new
{
sysproperties = ed.SystemProperties,
properties = ed.Properties,
body = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(ed.GetBytes()))
}
};
await client.PostAsJsonAsync("", new[] { egevent });
}

// helper
class HttpClientHelper
{
public static HttpClient Client(string address, string key)
{
var client = new HttpClient() { BaseAddress = new Uri(address) };
client.DefaultRequestHeaders.Add("aeg-sas-key", key);
return client;
}
}

函数.json:

    {
"bindings": [
{
"type": "eventHubTrigger",
"name": "ed",
"direction": "in",
"path": "myIoTHubName",
"connection": "connectionstringIOTHUB",
"consumerGroup": "eventing",
"cardinality": "many"
}
],
"disabled": false
}

请注意,AEG 自定义主题的负载取决于其inputSchema 属性。基本上,当前的 AEG 版本(还包括预览版)允许从以下选择中声明输入模式:

  • EventGridSchema(默认架构)
  • CloudEventV01架构
  • CustomEventSchema(仍在预览版中)

更多详情请参见:

关于azure - 如何在 Azure Function 中写入 Azure 事件网格主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52348802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com