Alessio Innocenzi
Alessio Innocenzi

Reputation: 461

How to get PartitionId in event hub triggered Azure function?

I have an Azure Function triggered by Event Hub. I configured the function app in the host.json with a MaxBatchSize of 32. My Event Hub has 6 partitions, and I don't want an event to be processed twice.

So, I need partition ID and sequence number to identify the event uniquely. I would like to save PartitionID and SequenceNumber in my database as primary keys. My function is triggered in input with Array of EventData. Iterating on the array, I can get the SequenceNumber for each message, but I don't know how to get per PartitionID. I tried to include parameter of type PartitionContext among input parameters but it doesn't work.

Here it is my code:

[FunctionName("EventHubTriggeredFunction")]
public static void Run([EventHubTrigger("events", Connection = "EventHubConnection")]EventData[] eventHubMessages, TraceWriter log)
{
    foreach (var message in eventHubMessages)
    {
        using (Stream stream = message.GetBodyStream())
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                try
                {
                    //... do something

                    //SequenceNumber: message.SequenceNumber
                    Save(reader.ReadToEnd(), message);

                    //... do something else
                }
            }
        }
 }

How can I get PartitionID for each message processed?

Upvotes: 2

Views: 3578

Answers (1)

Sreeram Garlapati
Sreeram Garlapati

Reputation: 4993

Define the EventHub trigger to include Event Metadata. Then use the receiverRuntimeInfo on the context object.

PartitionContext.RuntimeInfo.PartitionId

Upvotes: 4

Related Questions