Is there a way to get the headers data of events (EventHub) using @EventHubTrigger of Azure Functions in Java?

Tags: , , , ,



I have an Azure Function implemented with azure-functions-java-library that receives events from one EventHub and I’m using @EventHubTrigger, the problem is that I need the header data from an event but I don’t see any way to get this, I have already read the docs and nothing. The reason I need this is because from the EventHub I’am receiving events with different Avro schemas so I need to distinguish them in order to parse it.

I’d really appreciate some help.

Answer

Yes, you can retrieve message metadata by adding a @BindingName("Properties") annotation to a method parameter like below for example. Things to note here you can bind to any metadata of an event using binding expression. In this case, it’s “Properties”. Also, Cardinality should be ONE.

@FunctionName("EventHubExample")
    public void logEventHubMessage(
        @EventHubTrigger(name = "message", eventHubName = "test", connection = "AzureEventHubConnection", consumerGroup = "$Default", cardinality = Cardinality.ONE, dataType = "string") 
        String message,
        final ExecutionContext context,
        @BindingName("Properties")
        Map<String, Object> properties) {            
        context.getLogger().info("Event hub message received: " + message + ", properties: " + properties);
    }

I used Service Bus Explorer as Event Sender to set metadata of the event as below and was able to see those in the consumer side using above code in “Properties” binding. enter image description here

N.B. C# function SDK has a benefit here over Java. In C#, you can get the whole Event object which is easier to navigate for metadata directly while getting multiple events in input. But unfortunately, that’s not possible in Java SDK as of now where you have to bind separately with single cardinality.



Source: stackoverflow