Publish to an Azure Event Hub with output bindings and use partition keys

71 Views Asked by At

I'm publishing an object to an Azure Event Hub using output bindings like so.

[FunctionName("IntegrationFunction")]
[return: EventHub("%Transactions:EVH:Transactions:Hub%", Connection = "Transactions:EVH:Transactions")]
public Transaction Run(
    [ServiceBusTrigger(
        "%Transactions:SB:Transactions:ReceiveTopic%",
        "%Transactions:SB:Transactions:AnalyticsTopicSubscription%",
        Connection = "Transactions:SB:Transactions")]
    string mySbMsg,
    ILogger log)
{
    var retailTransaction = JsonConvert.DeserializeObject<RetailTransaction>(
        mySbMsg,
        JsonSerialisationUtils.SerialiserSettings);
    
    try
    {
        var transaction = retailTransaction.ToDto();
        log.LogInformation($"Transaction {transaction.TransactionNumber} processed.");
        return transaction;
    }
    catch (Exception e)
    {
        log.LogError(e, "Error mapping transaction.");
    }

    return null;
}

Is there any way to publish it to a specific partition e.g. transaction.StoreCode?

1

There are 1 best solutions below

0
Ikhtesam Afrin On

You can pass the partition key value while publishing the events to EventHub using IAsyncCollector<EventData>. Refer the MSDoc and github issue for more details.

  • I am using the below code to set the partition key.
using System;
using System.Text;
using System.Threading.Tasks;
using Azure.Messaging.EventHubs;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.EventHubs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace _78148689
{
    public class Function1
    {
        [FunctionName("IntegrationFunction")]
        public static async Task Run([ServiceBusTrigger("mytopic", "mysubscription", Connection = "serviceBusConnection")]string mySbMsg,
            [EventHub("test-hub", Connection = "EventHubConnectionAppSetting")]IAsyncCollector<EventData> outputEvents, ILogger log)
        {
            log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");

            var eventData = new EventData(Encoding.UTF8.GetBytes(mySbMsg));

            await outputEvents.AddAsync(eventData, "demo-key");
        }
    }
}

.csproj-

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <RootNamespace>_78148689</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Azure.Messaging.EventHubs" Version="5.11.1" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.EventHubs" Version="6.2.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.13.6" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.3.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

I am able to publish the event.

enter image description here

enter image description here