在 Azure 事件中樞內將自訂資料新增至事件

事件主要是由一組不透明的位元組所組成,因此這些事件的取用者可能很難做出明智的決策來進行處理。 為了讓事件發行者為取用者提供更完備的內容,事件也可包含自訂中繼資料,格式為一組索引鍵/值組。 包含中繼資料的其中一個常見案例,是提供事件所包含之資料類型的提示,讓取用者了解其格式,並可適當地還原序列化。

注意

此中繼資料不是由事件中樞服務使用,也不會以任何對此服務有意義的方式使用;唯一用途為在事件發行者與取用者之間進行協調。

下列各節說明如何使用不同的程式設計語言將自訂資料新增至事件。

.NET

var eventBody = new BinaryData("Hello, Event Hubs!");
var eventData = new EventData(eventBody);
eventData.Properties.Add("EventType", "com.microsoft.samples.hello-event");
eventData.Properties.Add("priority", 1);
eventData.Properties.Add("score", 9.0);

如需完整的程式碼範例,請參閱使用自訂中繼資料發佈事件

Java

EventData firstEvent = new EventData("EventData Sample 1".getBytes(UTF_8));
firstEvent.getProperties().put("EventType", "com.microsoft.samples.hello-event");
firstEvent.getProperties().put("priority", 1);
firstEvent.getProperties().put("score", 9.0);

如需完整的程式碼範例,請參閱使用自訂中繼資料發佈事件

Python

event_data = EventData('Message with properties')
event_data.properties = {'event-type': 'com.microsoft.samples.hello-event', 'priority': 1, "score": 9.0}

如需完整的程式碼範例,請參閱使用屬性傳送事件資料批次

JavaScript

let eventData = { body: "First event", properties: { "event-type": "com.microsoft.samples.hello-event", "priority": 1, "score": 9.0  } };

下一步

請參閱下列快速入門和範例。