【Azure 事件中心】从Azure Event Hub中消费数据,如何查看当前消费客户端消费数据的Offset和SequenceNumber呢(消息偏移量和序列号)?

问题描述

当通过Azure Event Hub SDK消费Event Hub中的消息时,必须指定一个Storage Account(存储账号)用于保存 Checkpoint (检查点)。

 

比如在C#代码中,需要指定Storage Account Connection String 和一个Blob Container的名称。其他语言代码也是一样,都需要指定Storage Account。

    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>";

...... 

  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 
        storageClient = new BlobContainerClient(blobStorageConnectionString, blobContainerName);

        // Create an event processor client to process events in the event hub
        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 30 seconds for the events to be processed
        await Task.Delay(TimeSpan.FromSeconds(30));

        // Stop the processing
        await processor.StopProcessingAsync();
    }

 

那么,在Storage Account中如何查看Offest 和 Sequence Number的值呢?

 

解答如下

 

 如上图,在Azure门户页面中,进入当前使用的Storage Account页面:

  1. 在指定的Container中,SDK会自动创建一个以Event Hub Namespace主机域名为名的Folder
  2. 然后是Event Hub -> 消费组(默认为 $default) --> Checkpint --> 分区号(从0开始,1,2 ...) 
  3. 在以分区号为名称的Blob的元数据(Metadata)记录了sequencenumber 和 offset的值。

 

PS: 当删除或修改这个值后,如果重启Event Hub的消费端,这会导致数据从新的OFFSET开始获取。

 

参考资料

Checkpoint: https://docs.azure.cn/zh-cn/event-hubs/event-processor-balance-partition-load#checkpointing

向 Azure 事件中心发送事件及从 Azure 事件中心接收事件 - .NET (Azure.Messaging.EventHubs): https://docs.azure.cn/zh-cn/event-hubs/event-hubs-dotnet-standard-getstarted-send#receive-events

 

posted @ 2022-08-17 19:36  路边两盏灯  阅读(204)  评论(0编辑  收藏  举报