| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Text; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using Azure.Messaging.EventHubs.Producer; |
| | 8 | | using Azure.Messaging.EventHubs.Samples.Infrastructure; |
| | 9 | |
|
| | 10 | | namespace Azure.Messaging.EventHubs.Samples |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// An introduction to publishing events, using a partition key to group batches together. |
| | 14 | | /// </summary> |
| | 15 | | /// |
| | 16 | | public class Sample06_PublishAnEventBatchWithPartitionKey : IEventHubsSample |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// The name of the sample. |
| | 20 | | /// </summary> |
| | 21 | | /// |
| 0 | 22 | | public string Name => nameof(Sample06_PublishAnEventBatchWithPartitionKey); |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// A short description of the sample. |
| | 26 | | /// </summary> |
| | 27 | | /// |
| 0 | 28 | | public string Description => "An introduction to publishing events, using a partition key to group batches toget |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Runs the sample using the specified Event Hubs connection information. |
| | 32 | | /// </summary> |
| | 33 | | /// |
| | 34 | | /// <param name="connectionString">The connection string for the Event Hubs namespace that the sample should tar |
| | 35 | | /// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that she sample should ru |
| | 36 | | /// |
| | 37 | | public async Task RunAsync(string connectionString, |
| | 38 | | string eventHubName) |
| | 39 | | { |
| | 40 | | // We will start by creating a producer client using its default set of options. |
| | 41 | |
|
| 0 | 42 | | await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName)) |
| | 43 | | { |
| | 44 | | // When publishing events, it may be desirable to request that the Event Hubs service keep the different |
| | 45 | | // event batches together on the same partition. This can be accomplished by setting a |
| | 46 | | // partition key when publishing the batch. |
| | 47 | | // |
| | 48 | | // The partition key is NOT the identifier of a specific partition. Rather, it is an arbitrary piece of |
| | 49 | | // that Event Hubs uses as the basis to compute a hash value. Event Hubs will associate the hash value |
| | 50 | | // partition, ensuring that any events published with the same partition key are routed to the same part |
| | 51 | | // |
| | 52 | | // Note that there is no means of accurately predicting which partition will be associated with a given |
| | 53 | | // we can only be assured that it will be a consistent choice of partition. If you have a need to under |
| | 54 | | // exact partition an event is published to, you will need to use an Event Hub producer associated with |
| | 55 | | // |
| | 56 | | // We will publish a small batch of events based on simple sentences. |
| | 57 | |
|
| | 58 | | // To choose a partition key, you will need to create a custom set of batch options. |
| | 59 | |
|
| 0 | 60 | | var batchOptions = new CreateBatchOptions |
| 0 | 61 | | { |
| 0 | 62 | | PartitionKey = "Any Value Will Do..." |
| 0 | 63 | | }; |
| | 64 | |
|
| 0 | 65 | | using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(batchOptions); |
| 0 | 66 | | eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Hello, Event Hubs!"))); |
| 0 | 67 | | eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Goodbye, Event Hubs!"))); |
| | 68 | |
|
| 0 | 69 | | await producerClient.SendAsync(eventBatch); |
| | 70 | |
|
| 0 | 71 | | Console.WriteLine("The event batch has been published."); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | // At this point, our client has passed its "using" scope and has safely been disposed of. We |
| | 75 | | // have no further obligations. |
| | 76 | |
|
| 0 | 77 | | Console.WriteLine(); |
| 0 | 78 | | } |
| | 79 | | } |
| | 80 | | } |