3

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.

javo
  • 43
  • 7

1 Answers1

7

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.

krishg
  • 5,935
  • 2
  • 12
  • 19
  • Hi, thank you for your answer, this "Properties" name it is proper to EventHubTrigger or it's a custom name defined by you ? – javo Sep 03 '20 at 17:00
  • "Properties" is one of the metadata of Event. Not defined by me. As I already shared the link in my answer. https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-hubs-trigger?tabs=java#event-metadata – krishg Sep 03 '20 at 17:04
  • Thank you a lot, it works ! but now I'm receiving the values of headers in some kind of encoding do you know any about that ? – javo Sep 03 '20 at 18:15
  • Can you give an example? – krishg Sep 03 '20 at 18:27
  • 1
    My friend I've already found the solution it results that because of the protocol AMQP it encodes the headers in base64. I spent a lot of time on this, thank you again. – javo Sep 03 '20 at 19:43
  • I believe you are talking about byte[] value? – krishg Sep 04 '20 at 08:28