gpt4 book ai didi

azure - 我们如何在Azure.Messaging.EventHubs中设置partitionkey

转载 作者:行者123 更新时间:2023-12-02 08:20:47 28 4
gpt4 key购买 nike

我正在尝试向 Eventhub 发送通知,之前我使用 https://www.nuget.org/packages/Microsoft.Azure.EventHubs/打包,现在我正在使用 https://www.nuget.org/packages/Azure.Messaging.EventHubs 来完成它。这是我正在使用的代码。它工作正常,但我不知道如何在发送消息时传递分区键,因为 eventData.PartitionKey = partitionKey; 属性是只读属性。

                var producerClient = new EventHubProducerClient(connectionString, eventHubName);                    
using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();
var eventData = new EventData(Encoding.UTF8.GetBytes(message));
eventData.PartitionKey = partitionKey;

if (!eventBatch.TryAdd(eventData))
throw new Exception($"batch size is large and cannot be sent");

await producerClient.SendAsync(eventBatch);
await producerClient.DisposeAsync();

最佳答案

为了提高效率,您将整批发送到同一分区,因此 PartitionKey 设置在 CreateBatchOptions 中,例如来自样本:

    var connectionString = "<< CONNECTION STRING FOR THE EVENT HUBS NAMESPACE >>";
var eventHubName = "<< NAME OF THE EVENT HUB >>";

var producer = new EventHubProducerClient(connectionString, eventHubName);

try
{
var batchOptions = new CreateBatchOptions
{
PartitionKey = "Any Value Will Do..."
};

using var eventBatch = await producer.CreateBatchAsync(batchOptions);

for (var index = 0; index < 5; ++index)
{
var eventBody = new BinaryData($"Event #{ index }");
var eventData = new EventData(eventBody);

if (!eventBatch.TryAdd(eventData))
{
throw new Exception($"The event at { index } could not be added.");
}
}

await producer.SendAsync(eventBatch);
}
finally
{
await producer.CloseAsync();
}

Publishing events with a partition key

关于azure - 我们如何在Azure.Messaging.EventHubs中设置partitionkey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68759685/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com