重要事項
僅淘汰 Microsoft Purview 資料控管功能
Microsoft Purview 統一目錄和傳統 Microsoft Purview 資料控管功能 (先前的 Azure Purview) 即將淘汰。 整個 Microsoft Purview 並未淘汰。 Microsoft Purview 資料安全性與合規性功能不受此公告影響,且 Microsoft 會繼續投資於此功能。
Microsoft 正在 Microsoft Fabric 中合併 其資料控管 功能。 無法再建立新的 Microsoft Purview 資料控管帳戶,而且現有的資料控管功能將維持在維護模式,直到 2027 年 11 月 12 日為止。
如需淘汰時間表、轉換指引,以及建議的後續步驟,請參閱 Microsoft Purview 資料控管淘汰公告。
本快速入門將告訴您如何傳送和接收 Atlas Kafka 主題事件。 我們會使用 Azure 事件中樞 和 Azure.Messaging.EventHubs .NET 程式庫。
必要條件
如果您不熟悉事件中樞,請參閱事件 中樞概觀 ,再完成本快速入門。
若要遵循本快速入門,您需要具備某些必要條件:
- Microsoft Azure 訂閱。 若要使用 Azure 服務,包括事件中樞,您需要 Azure 訂用帳戶。 如果您沒有 Azure 帳戶,您可以註冊免費 試用 版,或在 建立帳戶時使用 MSDN 訂閱者權益。
-
Microsoft Visual Studio 2022。 事件中樞用戶端程式庫會利用 C# 8.0 中引進的新功能。 您仍然可以在先前的 C# 版本中使用程式庫,但將無法使用新語法。 若要使用完整語法,建議您使用 .NET Core SDK 3.0 或更新版本進行編譯,且 語言版本 設定為
latest。 如果您使用的是 Visual Studio 2019 之前的 Visual Studio 版本,則它不具備組建 C# 8.0 專案所需的工具。 您可以在這裡下載 Visual Studio 2022,包括免費的社群版本。 - 有效的 Microsoft Purview 帳戶。
- 使用您的 Microsoft Purview 帳戶設定事件中樞,以傳送和接收訊息:
- 您的帳戶可能已設定。 您可以在 Azure 入口網站 的 [設定]、[ Kafka 設定] 底下查看您的 Microsoft Purview 帳戶。 如果尚未設定,請 遵循本指南。
將訊息發佈至 Microsoft Purview
讓我們建立 .NET Core 主控台應用程式,透過事件中樞將事件傳送至 Microsoft Purview Kafka 主題 ATLAS_HOOK。
若要將訊息發佈至 Microsoft Purview,您需要一個受管理的事件中樞,或至少一個具有勾點設定的事件中樞。
建立 Visual Studio 專案
接下來,在 Visual Studio 中建立 C# .NET 主控台應用程式:
- 啟動 Visual Studio。
- 在 [開始] 視窗中,選取 [建立新的專案>主控台應用程式] (.NET [Framework) ]。 需要 .NET 版本 4.5.2 或更新版本。
- 在 [專案名稱] 中,輸入 PurviewKafkaProducer。
- 選取 [建立] 以建立專案。
建立主控台應用程式
- 啟動 Visual Studio 2022。
- 選取 建立新專案。
- 在 [建立新專案 ] 對話方塊上,執行下列步驟: 如果沒有看到此對話方塊,請選取功能表上的 [ 檔案 ],選取 [新增],然後選取 [ 專案]。
- 選取 C# 做為程式設計語言。
- 針對應用程式的類型選取 [主控台 ]。
- 從結果清單中選取 [ 主控台應用程式] (.NET [核心) ]。
- 然後,選取 [下一步]。
新增事件中樞 NuGet 套件
從功能表中選取 [ 工具]、[>NuGet] 、[套件管理員>]、[套件管理員主控台 ]。
執行下列命令以安裝 Azure.Messaging.EventHubs NuGet 套件和 Azure.Messaging.EventHubs.Producer NuGet 套件:
Install-Package Azure.Messaging.EventHubsInstall-Package Azure.Messaging.EventHubs.Producer
撰寫傳送訊息至事件中樞的程式碼
將下列
using陳述式新增至 Program.cs 檔案的頂端:using System; using System.Text; using System.Threading.Tasks; using Azure.Messaging.EventHubs; using Azure.Messaging.EventHubs.Producer;將常數新增至
Program事件中樞連線字串和事件中樞名稱的類別。private const string connectionString = "<EVENT HUBS NAMESPACE - CONNECTION STRING>"; private const string eventHubName = "<EVENT HUB NAME>";將方法取代
Main為下列async Main方法,並新增要async ProduceMessage將訊息推送至 Microsoft Purview。 如需詳細資訊,請參閱程式碼中的註解。static async Task Main() { // Read from the default consumer group: $Default string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName; / Create an event producer client to add events in the event hub EventHubProducerClient producer = new EventHubProducerClient(ehubNamespaceConnectionString, eventHubName); await ProduceMessage(producer); } static async Task ProduceMessage(EventHubProducerClient producer) { // Create a batch of events using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(); // Add events to the batch. An event is a represented by a collection of bytes and metadata. eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("<First event>"))); eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("<Second event>"))); eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("<Third event>"))); // Use the producer client to send the batch of events to the event hub await producerClient.SendAsync(eventBatch); Console.WriteLine("A batch of 3 events has been published."); }建置專案。 確定沒有錯誤。
執行程式並等待確認訊息。
注意事項
如需包含更多資訊性註解的完整原始程式碼,請參閱 GitHub 中的此檔案
使用建立實體 JSON 訊息建立具有兩個欄的 SQL 資料表的範例程式碼
{
"msgCreatedBy":"nayenama",
"message":{
"type":"ENTITY_CREATE_V2",
"user":"admin",
"entities":{
"entities":[
{
"typeName":"azure_sql_table",
"attributes":{
"owner":"admin",
"temporary":false,
"qualifiedName":"mssql://nayenamakafka.eventhub.sql.net/salespool/dbo/SalesOrderTable",
"name":"SalesOrderTable",
"description":"Sales Order Table added via Kafka"
},
"relationshipAttributes":{
"columns":[
{
"guid":"-1102395743156037",
"typeName":"azure_sql_column",
"uniqueAttributes":{
"qualifiedName":"mssql://nayenamakafka.eventhub.sql.net/salespool/dbo/SalesOrderTable#OrderID"
}
},
{
"guid":"-1102395743156038",
"typeName":"azure_sql_column",
"uniqueAttributes":{
"qualifiedName":"mssql://nayenamakafka.eventhub.sql.net/salespool/dbo/SalesOrderTable#OrderDate"
}
}
]
},
"guid":"-1102395743156036",
"version":0
}
],
"referredEntities":{
"-1102395743156037":{
"typeName":"azure_sql_column",
"attributes":{
"owner":null,
"userTypeId":61,
"qualifiedName":"mssql://nayenamakafka.eventhub.sql.net/salespool/dbo/SalesOrderTable#OrderID",
"precision":23,
"length":8,
"description":"Sales Order ID",
"scale":3,
"name":"OrderID",
"data_type":"int"
},
"relationshipAttributes":{
"table":{
"guid":"-1102395743156036",
"typeName":"azure_sql_table",
"entityStatus":"ACTIVE",
"displayText":"SalesOrderTable",
"uniqueAttributes":{
"qualifiedName":"mssql://nayenamakafka.eventhub.sql.net/salespool/dbo/SalesOrderTable"
}
}
},
"guid":"-1102395743156037",
"version":2
},
"-1102395743156038":{
"typeName":"azure_sql_column",
"attributes":{
"owner":null,
"userTypeId":61,
"qualifiedName":"mssql://nayenamakafka.eventhub.sql.net/salespool/dbo/SalesOrderTable#OrderDate",
"description":"Sales Order Date",
"scale":3,
"name":"OrderDate",
"data_type":"datetime"
},
"relationshipAttributes":{
"table":{
"guid":"-1102395743156036",
"typeName":"azure_sql_table",
"entityStatus":"ACTIVE",
"displayText":"SalesOrderTable",
"uniqueAttributes":{
"qualifiedName":"mssql://nayenamakafka.eventhub.sql.net/salespool/dbo/SalesOrderTable"
}
}
},
"guid":"-1102395743156038",
"status":"ACTIVE",
"createdBy":"ServiceAdmin",
"version":0
}
}
}
},
"version":{
"version":"1.0.0"
},
"msgCompressionKind":"NONE",
"msgSplitIdx":1,
"msgSplitCount":1
}
接收 Microsoft Purview 訊息
接下來,學習如何撰寫 .NET Core 主控台應用程式,以使用事件處理器從事件中樞接收訊息。 事件處理器會管理持續性檢查點,以及來自事件中樞的平行接收。 這簡化了接收事件的過程。 您必須使用ATLAS_ENTITIES事件中樞,才能接收來自 Microsoft Purview 的訊息。
若要接收來自 Microsoft Purview 的訊息,您需要受管理的事件中樞或事件中樞通知設定。
警告
事件中樞 SDK 使用最新版本的可用儲存體 API。 您的 Stack Hub 平台不一定提供該版本。 如果您在 Azure Stack Hub 上執行此程式碼,除非您以使用的特定版本為目標,否則會遇到執行階段錯誤。 如果您使用 Azure Blob 儲存體做為檢查點存放區,請檢閱 Azure Stack Hub 組建支援的 Azure 儲存體 API 版本 ,並在程式碼中以該版本為目標。
儲存體服務的最高可用版本為 2019-02-02 版。 根據預設,事件中樞 SDK 用戶端程式庫會在 SDK) 發行時,使用 Azure (2019 年 7 月 7 日的最高可用版本。 如果您使用的是 Azure Stack Hub 版本 2005,除了遵循本節中的步驟之外,您還需要新增以儲存體服務 API 版本 2019-02-02 為目標的程式碼。 若要瞭解如何鎖定特定儲存體 API 版本,請參閱 GitHub 中的此範例。
建立 Azure 儲存體和 Blob 容器
我們會使用 Azure 儲存體做為檢查點存放區。 使用下列步驟建立 Azure 儲存帳戶。
-
記下連接字串和容器名稱。 您將在接收代碼中使用它們。
為接收者建立 Visual Studio 專案
- 在 [方案總管] 視窗中,選取並按住 (或以滑鼠右鍵按一下 EventHubQuickStart 解決方案) ,指向 [新增],然後選取 [新增專案]。
- 選取 [ 主控台應用程式] (.NET [核心) ],然後選取 [下一步]。
- 輸入 PurviewKafkaConsumer 作為 專案名稱,然後選取 [建立]。
新增事件中樞 NuGet 套件
從功能表中選取 [ 工具]、[>NuGet] 、[套件管理員>]、[套件管理員主控台 ]。
執行下列命令以安裝 Azure.Messaging.EventHubs NuGet 套件:
Install-Package Azure.Messaging.EventHubs執行下列命令以安裝 Azure.Messaging.EventHubs.Processor NuGet 套件:
Install-Package Azure.Messaging.EventHubs.Processor
更新 Main 方法
在Program.cs檔案的頂端新增下列
using語句。using System; using System.Text; using System.Threading.Tasks; using Azure.Storage.Blobs; using Azure.Messaging.EventHubs; using Azure.Messaging.EventHubs.Consumer; using Azure.Messaging.EventHubs.Processor;將常數新增至
Program事件中樞連線字串和事件中樞名稱的類別。 以您建立事件中樞和儲存體帳戶時取得的實際值取代方括號中的預留位置 (存取金鑰 - 主要連接字串) 。 請確定這是{Event Hubs namespace 連接字串}命名空間層級連接字串,而不是事件中樞字串。private const string ehubNamespaceConnectionString = "<EVENT HUBS NAMESPACE - CONNECTION STRING>"; private const string eventHubName = "<EVENT HUB NAME>"; private const string blobStorageConnectionString = "<AZURE STORAGE CONNECTION STRING>"; private const string blobContainerName = "<BLOB CONTAINER NAME>";傳送訊息至 Microsoft Purview 時,請使用 ATLAS_ENTITIES 做為事件中樞名稱。
將方法
Main取代為下列async Main方法。 如需詳細資訊,請參閱程式碼中的註解。static async Task Main() { // Read from the default consumer group: $Default string consumerGroup = EventHubConsumerClient.DefaultConsumerGroupName; // Create a blob container client that the event processor will use BlobContainerClient storageClient = new BlobContainerClient(blobStorageConnectionString, blobContainerName); // Create an event processor client to process events in the event hub EventProcessorClient processor = new EventProcessorClient(storageClient, consumerGroup, ehubNamespaceConnectionString, eventHubName); // Register handlers for processing events and handling errors processor.ProcessEventAsync += ProcessEventHandler; processor.ProcessErrorAsync += ProcessErrorHandler; // Start the processing await processor.StartProcessingAsync(); // Wait for 10 seconds for the events to be processed await Task.Delay(TimeSpan.FromSeconds(10)); // Stop the processing await processor.StopProcessingAsync(); }現在,將下列事件和錯誤處理常式方法新增至類別中。
static async Task ProcessEventHandler(ProcessEventArgs eventArgs) { // Write the body of the event to the console window Console.WriteLine("\tReceived event: {0}", Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray())); // Update checkpoint in the blob storage so that the app receives only new events the next time it's run await eventArgs.UpdateCheckpointAsync(eventArgs.CancellationToken); } static Task ProcessErrorHandler(ProcessErrorEventArgs eventArgs) { // Write details about the error to the console window Console.WriteLine($"\tPartition '{ eventArgs.PartitionId}': an unhandled exception was encountered. This was not expected to happen."); Console.WriteLine(eventArgs.Exception.Message); return Task.CompletedTask; }建置專案。 確定沒有錯誤。
注意事項
如需包含更多資訊性註解的完整原始程式碼,請參閱 GitHub 上的此檔案。
執行接收器應用程式。
從 Microsoft Purview 接收的郵件範例
{
"version":
{"version":"1.0.0",
"versionParts":[1]
},
"msgCompressionKind":"NONE",
"msgSplitIdx":1,
"msgSplitCount":1,
"msgSourceIP":"10.244.155.5",
"msgCreatedBy":
"",
"msgCreationTime":1618588940869,
"message":{
"type":"ENTITY_NOTIFICATION_V2",
"entity":{
"typeName":"azure_sql_table",
"attributes":{
"owner":"admin",
"createTime":0,
"qualifiedName":"mssql://nayenamakafka.eventhub.sql.net/salespool/dbo/SalesOrderTable",
"name":"SalesOrderTable",
"description":"Sales Order Table"
},
"guid":"ead5abc7-00a4-4d81-8432-d5f6f6f60000",
"status":"ACTIVE",
"displayText":"SalesOrderTable"
},
"operationType":"ENTITY_UPDATE",
"eventTime":1618588940567
}
}
後續步驟
在 GitHub 中查看更多範例。