| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Text; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Azure.Messaging.EventHubs.Producer; |
| | 9 | | using Azure.Messaging.EventHubs.Samples.Infrastructure; |
| | 10 | |
|
| | 11 | | namespace Azure.Messaging.EventHubs.Samples |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// An introduction to publishing events, specifying a specific partition for the batch to be published to. |
| | 15 | | /// </summary> |
| | 16 | | /// |
| | 17 | | public class Sample07_PublishAnEventBatchToASpecificPartition : IEventHubsSample |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// The name of the sample. |
| | 21 | | /// </summary> |
| | 22 | | /// |
| 0 | 23 | | public string Name => nameof(Sample07_PublishAnEventBatchToASpecificPartition); |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// A short description of the sample. |
| | 27 | | /// </summary> |
| | 28 | | /// |
| 0 | 29 | | public string Description => "An introduction to publishing events, specifying a specific partition for the batc |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Runs the sample using the specified Event Hubs connection information. |
| | 33 | | /// </summary> |
| | 34 | | /// |
| | 35 | | /// <param name="connectionString">The connection string for the Event Hubs namespace that the sample should tar |
| | 36 | | /// <param name="eventHubName">The name of the Event Hub, sometimes known as its path, that she sample should ru |
| | 37 | | /// |
| | 38 | | public async Task RunAsync(string connectionString, |
| | 39 | | string eventHubName) |
| | 40 | | { |
| | 41 | | // We will start by creating a producer client using its default set of options. |
| | 42 | |
|
| 0 | 43 | | await using (var producerClient = new EventHubProducerClient(connectionString, eventHubName)) |
| | 44 | | { |
| | 45 | | // To ensure that we request a valid partition, we'll need to read the metadata for the Event Hub. We w |
| | 46 | | // select the first available partition. |
| | 47 | |
|
| 0 | 48 | | string firstPartition = (await producerClient.GetPartitionIdsAsync()).First(); |
| | 49 | |
|
| | 50 | | // When publishing events, it may be desirable to request that the Event Hubs service place a batch on a |
| | 51 | | // for organization and processing. For example, you may have designated one partition of your Event Hu |
| | 52 | | // for all of your telemetry-related events. |
| | 53 | | // |
| | 54 | | // This can be accomplished by setting the identifier of the desired partition when creating the batch. |
| | 55 | | // that if you are using a partition identifier, you may not also specify a partition key; they are mutu |
| | 56 | | // |
| | 57 | | // We will publish a small batch of events based on simple sentences. |
| | 58 | |
|
| | 59 | | // To choose a partition identifier, you will need to create a custom set of batch options. |
| | 60 | |
|
| 0 | 61 | | var batchOptions = new CreateBatchOptions |
| 0 | 62 | | { |
| 0 | 63 | | PartitionId = firstPartition |
| 0 | 64 | | }; |
| | 65 | |
|
| 0 | 66 | | using EventDataBatch eventBatch = await producerClient.CreateBatchAsync(batchOptions); |
| 0 | 67 | | eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Hello, Event Hubs!"))); |
| 0 | 68 | | eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Goodbye, Event Hubs!"))); |
| | 69 | |
|
| 0 | 70 | | await producerClient.SendAsync(eventBatch); |
| | 71 | |
|
| 0 | 72 | | Console.WriteLine("The event batch has been published."); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | // At this point, our client has passed its "using" scope and has safely been disposed of. We |
| | 76 | | // have no further obligations. |
| | 77 | |
|
| 0 | 78 | | Console.WriteLine(); |
| 0 | 79 | | } |
| | 80 | | } |
| | 81 | | } |